Android常用控件(一)【RadioGroup、RadioButton、CheckBox、Toast】

  一、RadioGroup RadioButton 的组。一个 RadioGroup 里面可以有多个 RadioButton ,但是只能选中其中的一个 RadioButton

radio_checkbox.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />

    <RadioGroup
            android:id="@+id/genderGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
        <RadioButton
            android:id="@+id/femaleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female" />
        <RadioButton
            android:id="@+id/maleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male" />
    </RadioGroup>
    <CheckBox 
        android:id="@+id/swim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/swim" />
    <CheckBox
        android:id="@+id/run"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/run" />
    <CheckBox
        android:id="@+id/read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/read" />
</LinearLayout>
public class RadioCheckBox extends Activity {
    //对控件进行声明
     private RadioGroup genderGroup = null;
    private RadioButton femaleButton = null;
    private RadioButton maleButton = null;
    private CheckBox swimBox = null;
    private CheckBox runBox = null;
    private CheckBox readBox = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio_checkbox);
        genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
        femaleButton = (RadioButton) findViewById(R.id.femaleButton);
        maleButton = (RadioButton) findViewById(R.id.maleButton);
        swimBox = (CheckBox) findViewById(R.id.swim);
        runBox = (CheckBox) findViewById(R.id.run);
        readBox = (CheckBox) findViewById(R.id.read);
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    /*编写监听器,监听器实现的是RadioGroup.OnCheckedChangedChangeListener()提供的接口,需要重写里面的pubilc void onCheckedChanged(RadioGroup group,int chenkedId)方法,这个方法的第一个参数是用来接收RadioGroup对象,第二个参数是用来接收被选中的RadioButton的ID。*/
    public void onCheckedChanged(RadioGroup group, int checkedId) {
    if(femaleButton.getId() == checkedId){
     System.out.println("female");
     //Toast.makeText(RadioCheckBox.this, "female", Toast.LENGTH_SHORT).show();
    }else if(maleButton.getId() == checkedId){
     System.out.println("male");
    }
   }
  });

    /*为每一个CheckBox都编写一个监听器,该监听器实现的是CompoundButton.OnCheckedChangeListener()提供的接口,需要重写里面的public void onCheckedChange(CompoundButton buttonView,boolean isChecked)方法,这个方法的第一个参数是用来接收CompoundButton对象,第二个参数是用来接收是否被选中,在这个方法里面可以做一系列的判断和操作,比如判断某个CheckBox有没有被选中*/     
     swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
           System.out.println("swim is checked");
        }else{
           System.out.println("swim is unchecked");
      }
   }
  });
    runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            System.out.println("run is checked");
        }else{
            System.out.println("run is unchecked");
        }
      }
  });
    readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
           System.out.println("read is checked");
        }else{
           System.out.println("read is unchecked");
        }
     }
  });
 }
}

二、Toast的用法   

Toast主要用于提示信息,用起来很方便,一行代码就OK了。

1、创建Toast对象

   makeText(Context context, CharSequence text, int duration);

   通过调用这个方法,返回一个Toast对象。

   第一个参数是上下文对象,通常是你的应用程序或者Activity对象——类名.this,第二个参数就是要显示的文本内容,可以格式化文本,第三个参数是持续多长时间来显示消息,有两个常量:LENGTH_SHORT 或者LENGTH_LONG

2、调用show()方法显示。

      Toast.makeText(RadioCheckBox.this, "female", Toast.LENGTH_SHORT).show();

三:注意

RadioGroup的监听器实现是RadioGroup.OnCheckedChangeListener()提供的接口,而CheckBox的监听器实现的是CompoundButton.OnCheckedChangeListener()提供的接口。因为:在RadioGroup.OnCheckedChangeListener()里面的onCheckedChanged(RadioGroup group, int checkedId) 方法它里面需要的是一个RadioGroup参数,而RadioGroup并不是CompoundButton的子类,所以它只能再重新写一个这样的接口。CheckBox它本身就是CompoundButton的子类,所以它可以直接使用这个接口。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值