单选 和 复选

  布局文件  


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".RadioTest" >


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <RadioGroup android:id="@+id/genderGroup"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                >
        <RadioButton
                     android:id="@+id/womanbutton"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/woman"/>
        <RadioButton
                     android:id="@+id/manbutton"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:text="@string/man"/>
               
    </RadioGroup>
    
    <CheckBox
        android:id="@+id/seven"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/seven"/>
    <CheckBox
        android:id="@+id/lmz"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/lmz"/>
    <CheckBox
        android:id="@+id/love"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/love"/>


</LinearLayout>



java 文件


package seven.activity06;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


public class RadioTest extends Activity {
    // 对控件对像进行声明
private RadioGroup genderGroup = null;
private RadioButton manbutton = null;
private RadioButton womanbutton = null;
private CheckBox seven = null;
private CheckBox lmz = null;
private CheckBox love = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//  通过控件的ID 来得到代表控件的对象
genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
womanbutton = (RadioButton)findViewById(R.id.womanbutton);
manbutton = (RadioButton)findViewById(R.id.manbutton);
seven = (CheckBox)findViewById(R.id.seven);
lmz = (CheckBox)findViewById(R.id.lmz);
love = (CheckBox)findViewById(R.id.love);

// 为 radiogroup 设置监听器。 需要注意的是。这里的监听器有所不同。
// 是在该按钮的状态被改变以后调用该函数。
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(womanbutton.getId() == checkedId)
{
System.out.println("woman");
Toast.makeText(RadioTest.this, "woman", Toast.LENGTH_SHORT).show();
}
else if(manbutton.getId() == checkedId)
{
Toast.makeText(RadioTest.this, "man", Toast.LENGTH_SHORT).show();
}
}
});

//  为多选按钮添加监听器
seven.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(RadioTest.this, "已选择", Toast.LENGTH_SHORT).show();
}
}
//  接下来如上,的监听如上    不再写下去 
});
}


}











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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值