单选框、复选框响应事件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <RadioGroup
  9. android:id="@+id/radioGroup"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content">
  12. <RadioButton
  13. android:id="@+id/radiobtn1"
  14. android:text="单选框1"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content" />
  17. <RadioButton
  18. android:id="@+id/radiobtn2"
  19. android:text="单选框2"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content" />
  22. </RadioGroup>
  23. <CheckBox
  24. android:id="@+id/checkboxbtn1"
  25. android:text="复选框"
  26. android:checked="true"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content" />
  29. </LinearLayout>
  1. package com.bluelesson.no1app;
  2. import android.support.v7.app.AlertDialog;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.CheckBox;
  8. import android.widget.CompoundButton;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.Toast;
  12. public class MainActivity extends AppCompatActivity{
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. //初始化控件
  18. InitCtrl();
  19. }
  20. private void InitCtrl() {
  21. //初始化单选框和复选框
  22. RadioGroup radioGroup = findViewById(R.id.radioGroup);
  23. radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  24. @Override
  25. public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
  26. Toast.makeText(MainActivity.this,"单选框改变"+checkId,Toast.LENGTH_SHORT).show();
  27. }
  28. });
  29. // RadioButton radioButton1 = findViewById(R.id.radiobtn1);
  30. // radioButton1.setChecked(true);
  31. // radioButton1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  32. // @Override
  33. // public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  34. //
  35. // Toast.makeText(MainActivity.this,"单选框改变"+compoundButton.getId(),Toast.LENGTH_SHORT).show();
  36. // }
  37. // });
  38. CheckBox checkBox = findViewById(R.id.checkboxbtn1);
  39. checkBox.setChecked(true);
  40. checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  41. @Override
  42. public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
  43. Toast.makeText(MainActivity.this,"复选框改变",Toast.LENGTH_SHORT).show();
  44. }
  45. });
  46. }
  47. }

开关按钮响应事件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:orientation="vertical"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8. <ToggleButton
  9. android:id="@+id/togglebtn1"
  10. android:textOn="开"
  11. android:textOff="关"
  12. android:checked="true"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content" />
  15. </LinearLayout>
  1. package com.bluelesson.no1app;
  2. import android.support.v7.app.AlertDialog;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.CheckBox;
  8. import android.widget.CompoundButton;
  9. import android.widget.RadioButton;
  10. import android.widget.RadioGroup;
  11. import android.widget.Toast;
  12. import android.widget.ToggleButton;
  13. public class MainActivity extends AppCompatActivity{
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. //初始化控件
  19. InitCtrl();
  20. }
  21. private void InitCtrl() {
  22. ToggleButton toggleButton = findViewById(R.id.togglebtn1);
  23. toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  24. @Override
  25. public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
  26. Toast.makeText(MainActivity.this,"开关按钮"+isChecked,Toast.LENGTH_SHORT).show();
  27. }
  28. });
  29. }
  30. }

进度条响应事件

image.png

  1. <SeekBar
  2. android:id="@+id/seekbar"
  3. android:max="100"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content" />
  6. <TextView
  7. android:id="@+id/tv1"
  8. android:text=" "
  9. android:layout_gravity="center"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content" />
  1. TextView mtextView;
  2. private void InitCtrl() {
  3. mtextView = findViewById(R.id.tv1);
  4. SeekBar seekBar = findViewById(R.id.seekbar);
  5. seekBar.setProgress(60);
  6. //seekBar.setMax(100);
  7. seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  8. @Override
  9. public void onProgressChanged(SeekBar seekBar, int progreess, boolean b) {
  10. mtextView.setText("当前进度:"+progreess);
  11. }
  12. @Override
  13. public void onStartTrackingTouch(SeekBar seekBar) {
  14. Toast.makeText(MainActivity.this,"开始:",Toast.LENGTH_SHORT).show();
  15. }
  16. @Override
  17. public void onStopTrackingTouch(SeekBar seekBar) {
  18. Toast.makeText(MainActivity.this,"结束:",Toast.LENGTH_SHORT).show();
  19. }
  20. });