事件(2)

单选发生变化

OnCheckedChangeListener
  rg=(RadioGroup) findViewById(R.id.rge1);
  rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
      RadioButton rb=(RadioButton) findViewById(checkedId);
      String str=rb.getText().toString();
      tv.setText(str);
    }
  });

 1 public class Click extends Activity{
 2     private Button bt;            //定义按钮
 3     private TextView tv;        //定义信息显示组件
 4     private EditText et;        //定义文本输入组件
 5     
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.event);
 9         et=(EditText)findViewById(R.id.ete1);            //取得文本编辑组件
10         bt=(Button)findViewById(R.id.bte1);                //取得按钮
11         tv=(TextView)findViewById(R.id.tve1);            //取得文本显示
12         
13         rg=(RadioGroup) findViewById(R.id.rge1);
14         rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
15             
16             @Override
17             public void onCheckedChanged(RadioGroup group, int checkedId) {
18                 RadioButton rb=(RadioButton) findViewById(checkedId);
19                 String str=rb.getText().toString();
20                 tv.setText(str);
21             }
22         });
23 }
代码示例
 1 <RadioGroup
 2           android:id="@+id/rge1"
 3         android:layout_width="fill_parent" 
 4         android:layout_height="wrap_content" 
 5       />
 6       
 7       <RadioButton
 8           android:id="@+id/rbe1"
 9         android:layout_width="fill_parent" 
10         android:layout_height="wrap_content" 
11           android:text="香蕉"
12       >
13       <RadioButton
14           android:id="@+id/rbe2"
15         android:layout_width="fill_parent" 
16         android:layout_height="wrap_content" 
17           android:text="苹果"
18       />
19       </RadioButton>
20        
21       <TextView
22           android:id="@+id/tve1"
23         android:layout_width="fill_parent" 
24         android:layout_height="wrap_content" 
25            android:background="#FF0000"
26       />
xml文件

 

posted @ 2017-06-07 16:41  眼泪,还是流了  阅读(112)  评论(0编辑  收藏  举报