android基本控件RadioButton/CheckBox

MainActivity.xml

<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"
    tools:context="${relativePackage}.${activityClass}" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="vertical" >


        <RadioButton
            android:id="@+id/female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/female" >
        </RadioButton>


        <RadioButton
            android:id="@+id/male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/male" >
        </RadioButton>
    </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/write"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/write" />

</LinearLayout>


MainActivity.java



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


public class MainActivity extends Activity {
private  RadioGroup radioGroup;
private  RadioButton female;
private RadioButton male;
private CheckBox run,swim,write;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
female=(RadioButton)findViewById(R.id.female);
male=(RadioButton)findViewById(R.id.male);
run=(CheckBox)findViewById(R.id.run);
swim=(CheckBox)findViewById(R.id.swim);
write=(CheckBox)findViewById(R.id.write);

/*监听器*/
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){


@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(female.getId() == checkedId){
System.out.println("女生");
Toast.makeText(MainActivity.this, "女生", Toast.LENGTH_LONG).show(); 
}
else if(male.getId()==checkedId){
System.out.println("男生");
Toast.makeText(MainActivity.this, "男生", Toast.LENGTH_LONG).show(); 
}

} 
});

/*给每个checkBox都要加监听器*/
run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){


@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "run", Toast.LENGTH_LONG).show();
}

}

});

write.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){


@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "write", Toast.LENGTH_LONG).show();
}

}

});

swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){


@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "swim", Toast.LENGTH_LONG).show();
}

}

});


}
}



string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Activity_04</string>
    <string name="hello_world">单选和多选</string>
    <string name="female">female</string>
    <string name="male">male</string>
      <string name="swim">swim</string>
        <string name="run">run</string>
          <string name="write">write</string>

</resources>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值