CheckBox事件


activity_main.xml的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
   
    
          <CheckBox
              android:id="@+id/swim"
             android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:text="@string/swim"
             android:checked="true"
            
             />
         
          <CheckBox
              android:id="@+id/xiaqi"
              android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:text="@string/xiaqi" />
         
             <CheckBox
                 android:id="@+id/look"
              android:layout_width="wrap_content"
             android:layout_height="match_parent"
             android:text="@string/look" />
         
        <RadioGroup
            android:id="@+id/RGroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="请选择你的性别"
                android:singleLine="true"
                />
            <RadioButton
                android:id="@+id/man"
                android:layout_width="50dp"
                android:layout_height="match_parent"
    android:text="男"
    android:checked="true"
                />
            <RadioButton
                android:id="@+id/women"
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:text="女"
                />
        </RadioGroup>

</LinearLayout>

 

 

 

MainActivity.java中的代码


package com.example.editview;

import android.os.Bundle;

import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity
{
 private RadioGroup Rgroup=null;
 private RadioButton man=null;
 private  RadioButton wommen=null;
 @Override
    public void onCreate(Bundle savedInstanceState)
 {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Button bt;
//        Button bt1;
//        bt=(Button)findViewById(R.id.btn);
//        bt1=(Button)findViewById(R.id.btn1);
//  bt.setOnClickListener(listener1);
//        bt1.setOnClickListener(listener1);
       
        CheckBox bb;
        CheckBox cc;
        CheckBox dd;
        bb=(CheckBox)findViewById(R.id.swim);
        cc=(CheckBox)findViewById(R.id.xiaqi);
        dd=(CheckBox)findViewById(R.id.look);

        bb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
   {
    // TODO Auto-generated method stub
    if(isChecked)
    {  
     Toast.makeText(MainActivity.this, "你选择了游泳", Toast.LENGTH_LONG).show();
    }
    else
    {
     Toast.makeText(MainActivity.this, "你没有选择游泳", Toast.LENGTH_LONG).show();
    }
   }
        });
       
        cc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
   {
    // TODO Auto-generated method stub
    if(isChecked)
    {
     Toast.makeText(MainActivity.this, "你选择了下棋", Toast.LENGTH_LONG).show();
    }
    else
    {
     
     Toast.makeText(MainActivity.this, "你没选择了下棋", Toast.LENGTH_LONG).show();
    }
   }
  });
     
        dd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
   {
    // TODO Auto-generated method stub
    if(isChecked)
    {
     Toast.makeText(MainActivity.this, "你选择了看电视", Toast.LENGTH_LONG).show();
    }
    else
    {
     
     Toast.makeText(MainActivity.this, "你没选择了看电视", Toast.LENGTH_LONG).show();
    }
   }
  });
       
         
        Rgroup =(RadioGroup)findViewById(R.id.RGroup);
        man=(RadioButton)findViewById(R.id.man);
        wommen=(RadioButton)findViewById(R.id.women);
        Rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
   
   @Override
  public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub
   if(arg1==man.getId())
   {
     System.out.println("man");
   }
   else if (arg1==wommen.getId())
   {
     
    System.out.println("wommen");
   }
  }
  });
       
       
 }
 
}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值