android控制項開發之Radio(選項按鈕)和CheckBox(多選按鈕)開發

來源:互聯網
上載者:User

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




 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.