android控制項開發之Radio(選項按鈕)和CheckBox(多選按鈕)開發
android控制項開發之Radio(選項按鈕)和CheckBox(多選按鈕)開發本博文主要講述的是android開發中的單選和多選按鈕的使用,具體情況請看執行個體代碼:
MainActivity.java:
package com.example.radiotest;
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;
}
}
布局檔案main.xml:
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 >
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=@string/hello_world />
android:id=@+id/mySexGroup
android:layout_width=wrap_content
android:layout_height=wrap_content
>
android:id=@+id/maleButton
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=@string/male/>
android:id=@+id/femaleButton
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=@string/female/>
android:id=@+id/swinBox
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=@string/swin/>
android:id=@+id/runBox
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=@string/run/>
android:id=@+id/jumpBox
android:layout_width=wrap_content
android:layout_height=wrap_content
android:text=@string/jump/>