常用控件(1)—RadioGroup、CheckBox、Toast

MainActivity:

 1 package com.example.kj;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 import android.widget.CheckBox;
 7 import android.widget.CompoundButton;
 8 import android.widget.RadioButton;
 9 import android.widget.RadioGroup;
10 import android.widget.Toast;
11 
12 public class MainActivity extends Activity {
13 
14     private RadioGroup g1 = null;
15     private RadioButton B1 = null;
16     private CheckBox cb = null;
17 
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22         g1 = (RadioGroup) findViewById(R.id.radiogroup);
23         B1 = (RadioButton) findViewById(R.id.B1);
24         cb = (CheckBox) findViewById(R.id.CB);
25         
26         //绑定一个listenner在一个CheckBox上
27         cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
28             
29             @Override
30             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
31                 // TODO Auto-generated method stub
32                 if(isChecked==true){
33                     //用Toast显示(小提示显示)
34                     Toast.makeText(MainActivity.this, "CB!", Toast.LENGTH_SHORT).show();
35                     
36                 }
37                 
38             }
39         });
40 
41         g1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
42 
43             public void onCheckedChanged(RadioGroup group, int checkedId) {
44                 // TODO Auto-generated method stub
45                 if (B1.getId() == checkedId) {
46                     System.out.println("B1 Has Been Checked!");
47                     Toast.makeText(MainActivity.this, "B1!", Toast.LENGTH_SHORT)
48                             .show();
49                 }
50 
51             }
52         });
53     }
54 
55     @Override
56     public boolean onCreateOptionsMenu(Menu menu) {
57         // Inflate the menu; this adds items to the action bar if it is present.
58         getMenuInflater().inflate(R.menu.activity_main, menu);
59         return true;
60     }
61 
62 }

Main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context=".MainActivity" >
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="@string/hello_world" />
12 
13     <RadioGroup
14         android:id="@+id/radiogroup"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:orientation="vertical" >
18 
19         <RadioButton
20             android:id="@+id/B1"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="@string/a" />
24 
25         <RadioButton
26             android:id="@+id/B2"
27             android:layout_width="wrap_content"
28             android:layout_height="wrap_content"
29             android:text="@string/b" />
30 
31         <RadioButton
32             android:id="@+id/B3"
33             android:layout_width="wrap_content"
34             android:layout_height="wrap_content"
35             android:text="@string/c" />
36 
37         <RadioButton
38             android:id="@+id/B4"
39             android:layout_width="wrap_content"
40             android:layout_height="wrap_content"
41             android:text="@string/d" />
42     </RadioGroup>
43     
44     <CheckBox 
45             android:id="@+id/CB"
46             android:layout_width="wrap_content"
47             android:layout_height="wrap_content"
48             android:text="@string/CB"
49         />
50 
51 </LinearLayout>
原文地址:https://www.cnblogs.com/humanchan/p/3020805.html