Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android控件開發之Radio(單選按鈕)和CheckBox(多選按鈕)開發

android控件開發之Radio(單選按鈕)和CheckBox(多選按鈕)開發

編輯:關於Android編程

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/>




 


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved