Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207995
  • 博文数量: 102
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1015
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-05 16:45
文章存档

2014年(73)

2013年(29)

我的朋友

分类: Android平台

2013-12-15 21:16:17

01_12_Android常见控件(二) (RadioGroup、RadioButton、CheckBox、Toast)

1.RadioGroup和RadioButton的使用方法
2.CheckBox的使用方法
3.Toast的使用方法

点击(此处)折叠或打开

  1. //main.xml
  2. <LinearLayout xmlns:android=""
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <TextView
  8.         android:id="@+id/textView1"
  9.         android:layout_width="fill_parent"
  10.         android:layout_height="wrap_content"
  11.         android:text="@string/hello"
  12.         />
  13.     
  14.     <RadioGroup
  15.         android:id="@+id/genderGroup"
  16.         android:layout_width="wrap_content"
  17.         android:layout_height="wrap_content"
  18.         android:orientation="vertical"
  19.         >
  20.         <RadioButton
  21.             android:id="@+id/femaleButton"
  22.             android:layout_width="wrap_content"
  23.             android:layout_height="wrap_content"
  24.             android:text="@string/female"
  25.             >
  26.         </RadioButton>
  27.         <RadioButton
  28.             android:id="@+id/maleButton"
  29.             android:layout_width="wrap_content"
  30.             android:layout_height="wrap_content"
  31.             android:text="@string/male"
  32.             >
  33.         </RadioButton>
  34.     </RadioGroup>
  35.     
  36.     <CheckBox
  37.         android:id="@+id/swim"
  38.         android:layout_width="wrap_content"
  39.         android:layout_height="wrap_content"
  40.         android:text="@string/swim"
  41.         />
  42.     <CheckBox
  43.         android:id="@+id/run"
  44.         android:layout_width="wrap_content"
  45.         android:layout_height="wrap_content"
  46.         android:text="@string/run"
  47.         />
  48.     <CheckBox
  49.         android:id="@+id/read"
  50.         android:layout_width="wrap_content"
  51.         android:layout_height="wrap_content"
  52.         android:text="@string/read"
  53.         />
  54. </LinearLayout>


点击(此处)折叠或打开

  1. //RodioTest.java
  2. package com.lwb.myactivity_07;

  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.widget.CheckBox;
  6. import android.widget.CompoundButton;
  7. import android.widget.RadioButton;
  8. import android.widget.RadioGroup;
  9. import android.widget.Toast;

  10. public class RodioTest extends Activity {

  11.     private RadioGroup genderGroup=null;
  12.     private RadioButton femaleButton=null;
  13.     private RadioButton maleButton=null;
  14.     
  15.     private CheckBox swimBox=null;
  16.     private CheckBox runBox=null;
  17.     private CheckBox readBox=null;
  18.     
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.         
  24.         //通过控件的ID来得到代表空间的对象
  25.         genderGroup=(RadioGroup)findViewById(R.id.genderGroup);
  26.         femaleButton=(RadioButton)findViewById(R.id.femaleButton);
  27.         maleButton=(RadioButton)findViewById(R.id.maleButton);
  28.         
  29.         swimBox=(CheckBox)findViewById(R.id.swim);
  30.         runBox=(CheckBox)findViewById(R.id.run);
  31.         readBox=(CheckBox)findViewById(R.id.read);
  32.         
  33.         //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button空间的监听器是不同的
  34.         genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  35.             
  36.             @Override
  37.             public void onCheckedChanged(RadioGroup group, int checkedId) {
  38.                 // TODO Auto-generated method stub
  39.                 if(femaleButton.getId()==checkedId){
  40.                     System.out.println("female");
  41.                     //Toast.makeText(context, text, duration)
  42.                     Toast.makeText(RodioTest.this, "female", Toast.LENGTH_SHORT).show();
  43.                 }
  44.                 else if(maleButton.getId()==checkedId){
  45.                     System.out.println("male");
  46.                     Toast.makeText(RodioTest.this,"male",Toast.LENGTH_SHORT).show();
  47.                 }                
  48.             }
  49.         });
  50.         
  51.         //为多选按钮添加监听器
  52.         swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {        
  53.             
  54.                 @Override
  55.                 public void onCheckedChanged(CompoundButton buttonView,
  56.                         boolean isChecked) {
  57.                     // TODO Auto-generated method stub
  58.                     if(isChecked){
  59.                         System.out.println("swim is checked");
  60.                     }
  61.                     else{
  62.                         System.out.println("swim is unchecked");
  63.                     }
  64.                 }            
  65.         });
  66.         
  67.         runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  68.             
  69.             
  70.             @Override
  71.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  72.                 // TODO Auto-generated method stub
  73.                 if(isChecked){
  74.                     System.out.println("run is checked");
  75.                 }
  76.                 else{
  77.                     System.out.println("run is unchecked");
  78.                 }
  79.             }
  80.         });
  81.         
  82.         readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  83.             
  84.             
  85.             @Override
  86.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  87.                 // TODO Auto-generated method stub
  88.                 if(isChecked){
  89.                     System.out.println("read is checked");
  90.                 }
  91.                 else{
  92.                     System.out.println("read is unchecked");
  93.                 }
  94.             }
  95.         });
  96.         
  97.     }

  98. /*    @Override
  99.     public boolean onCreateOptionsMenu(Menu menu) {
  100.         // Inflate the menu; this adds items to the action bar if it is present.
  101.         getMenuInflater().inflate(R.menu.rodio_test, menu);
  102.         return true;
  103.     }*/

  104. }
1.RadioGroup和RadioButton的使用方法

点击(此处)折叠或打开

  1. <RadioGroup
  2.         android:id="@+id/genderGroup"
  3.         android:layout_width="wrap_content"
  4.         android:layout_height="wrap_content"
  5.         android:orientation="vertical"
  6.         >
  7.         <RadioButton
  8.             android:id="@+id/femaleButton"
  9.             android:layout_width="wrap_content"
  10.             android:layout_height="wrap_content"
  11.             android:text="@string/female"
  12.             >
  13.         </RadioButton>
  14.         <RadioButton
  15.             android:id="@+id/maleButton"
  16.             android:layout_width="wrap_content"
  17.             android:layout_height="wrap_content"
  18.             android:text="@string/male"
  19.             >
  20.         </RadioButton>
  21.     </RadioGroup>

点击(此处)折叠或打开

  1.     private RadioGroup genderGroup=null;
  2.     private RadioButton femaleButton=null;
  3.     private RadioButton maleButton=null;

  4.         //通过控件的ID来得到代表空间的对象
  5.         genderGroup=(RadioGroup)findViewById(R.id.genderGroup);
  6.         femaleButton=(RadioButton)findViewById(R.id.femaleButton);
  7.         maleButton=(RadioButton)findViewById(R.id.maleButton);

  8.         //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button空间的监听器是不同的
  9.         genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  10.             
  11.             @Override
  12.             public void onCheckedChanged(RadioGroup group, int checkedId) {
  13.                 // TODO Auto-generated method stub
  14.                 if(femaleButton.getId()==checkedId){
  15.                     System.out.println("female");
  16.                     //Toast.makeText(context, text, duration)
  17.                     Toast.makeText(RodioTest.this, "female", Toast.LENGTH_SHORT).show();
  18.                 }
  19.                 else if(maleButton.getId()==checkedId){
  20.                     System.out.println("male");
  21.                     Toast.makeText(RodioTest.this,"male",Toast.LENGTH_SHORT).show();
  22.                 }                
  23.             }
  24.         });
2.CheckBox的使用方法

点击(此处)折叠或打开

  1. <CheckBox
  2.         android:id="@+id/swim"
  3.         android:layout_width="wrap_content"
  4.         android:layout_height="wrap_content"
  5.         android:text="@string/swim"
  6.         />
  7.     <CheckBox
  8.         android:id="@+id/run"
  9.         android:layout_width="wrap_content"
  10.         android:layout_height="wrap_content"
  11.         android:text="@string/run"
  12.         />
  13.     <CheckBox
  14.         android:id="@+id/read"
  15.         android:layout_width="wrap_content"
  16.         android:layout_height="wrap_content"
  17.         android:text="@string/read"
  18.         />

点击(此处)折叠或打开

  1. private CheckBox swimBox=null;
  2.     private CheckBox runBox=null;
  3.     private CheckBox readBox=null;

  4.         swimBox=(CheckBox)findViewById(R.id.swim);
  5.         runBox=(CheckBox)findViewById(R.id.run);
  6.         readBox=(CheckBox)findViewById(R.id.read);


  7.         //为多选按钮添加监听器
  8.         swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {        
  9.             
  10.                 @Override
  11.                 public void onCheckedChanged(CompoundButton buttonView,
  12.                         boolean isChecked) {
  13.                     // TODO Auto-generated method stub
  14.                     if(isChecked){
  15.                         System.out.println("swim is checked");
  16.                     }
  17.                     else{
  18.                         System.out.println("swim is unchecked");
  19.                     }
  20.                 }            
  21.         });
  22.         
  23.         runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  24.             
  25.             
  26.             @Override
  27.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  28.                 // TODO Auto-generated method stub
  29.                 if(isChecked){
  30.                     System.out.println("run is checked");
  31.                 }
  32.                 else{
  33.                     System.out.println("run is unchecked");
  34.                 }
  35.             }
  36.         });
  37.         
  38.         readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  39.             
  40.             
  41.             @Override
  42.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  43.                 // TODO Auto-generated method stub
  44.                 if(isChecked){
  45.                     System.out.println("read is checked");
  46.                 }
  47.                 else{
  48.                     System.out.println("read is unchecked");
  49.                 }
  50.             }
  51.         });

3.Toast的使用方法

点击(此处)折叠或打开

  1. Toast.makeText(RodioTest.this, "female", Toast.LENGTH_SHORT).show();
这里就是几个简单控件的使用
阅读(473) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~