android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发
android控件开发之Radio(单选按钮)和CheckBox(多选按钮)开发
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 MainActivity extends Activity {
private RadioGroup generGroup = null;
private RadioButton maleButton = null;
private RadioButton femaleButton =null;
private CheckBox swin = null, run = null, jump = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
generGroup = (RadioGroup)findViewById(R.id.mySexGroup);
maleButton = (RadioButton)findViewById(R.id.maleButton);
femaleButton = (RadioButton)findViewById(R.id.femaleButton);
swin = (CheckBox)findViewById(R.id.swinBox);
run = (CheckBox)findViewById(R.id.runBox);
jump = (CheckBox)findViewById(R.id.jumpBox);
//设置RadioGroup监听器
generGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId == maleButton.getId()){
Toast.makeText(MainActivity.this, "male", Toast.LENGTH_SHORT).show();
}
else if(checkedId == femaleButton.getId()){
Toast.makeText(MainActivity.this, "female", Toast.LENGTH_SHORT).show();
}
}
});
//设置CheckBox多选的监听器
swin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("swin is checked!");
}
else{
System.out.println("swin is unchecked!");
}
}
});
run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("run is checked!");
}
else{
System.out.println("run is unchecked!");
}
}
});
jump.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("jump is checked!");
}
else{
System.out.println("jump is unchecked!");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RadioGroup
android:id="@+id/mySexGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"/>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"/>
</RadioGroup>
<CheckBox
android:id="@+id/swinBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swin"/>
<CheckBox
android:id="@+id/runBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run"/>
<CheckBox
android:id="@+id/jumpBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jump"/>
</LinearLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。