Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android初級教程小案例之單選框RadioGroup與復選框CheckBox

Android初級教程小案例之單選框RadioGroup與復選框CheckBox

編輯:關於Android編程

Android裡面的單選框和html中的其實是一樣的效果。這裡用到兩個控件:CheckBox和RadioGroup。直接上代碼:

radio.xml布局文件:

 





    
    





 

\
String.xml代碼:

 



    Hello World, Activity07!
    activity07
    
    
    swim
    run
    read

RadioTest:

 

 

package mars.activity07;

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 RadioTest extends Activity {
    /** Called when the activity is first created. */
	//對控件對象進行聲明
	private RadioGroup genderGroup = null;
	private RadioButton femaleButton = null;
	private RadioButton maleButton = null;
	private CheckBox swimBox = null;
	private CheckBox runBox = null;
	private CheckBox readBox = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio);
        //通過控件的ID來得到代表控件的對象
        genderGroup = (RadioGroup)findViewById(R.id.genderGroup);
        femaleButton = (RadioButton)findViewById(R.id.femaleButton);
        maleButton = (RadioButton)findViewById(R.id.maleButton);
        swimBox = (CheckBox)findViewById(R.id.swim);
        runBox = (CheckBox)findViewById(R.id.run);
        readBox = (CheckBox)findViewById(R.id.read);
        //為RadioGroup設置監聽器,需要注意的是,這裡的監聽器和Button控件的監聽器有所不同
        genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(femaleButton.getId() == checkedId){
					System.out.println("famale");
					Toast.makeText(RadioTest.this, "famle", Toast.LENGTH_SHORT).show();
				}
				else if(maleButton.getId() == checkedId)
				{
					System.out.println("male");
					Toast.makeText(RadioTest.this, "male", Toast.LENGTH_SHORT).show();
				}
			}
		});
        
        //為多選按鈕添加監聽器
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)//未選中到選中狀態是執行這裡:
				{
					System.out.println("swim is checked");
				}
				else//由選中狀態到未選中狀態時候執行這裡:
				{
					System.out.println("swim is unchecked");
				}
			}
		});
        runBox.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");
				}
			}
		});
        readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked)
				{
					System.out.println("read is checked");
				}
				else
				{
					System.out.println("read is unchecked");
				}
			}
		});
    }
    
}


 

注冊文件進行注冊:

 


        
            
                
                
            
        

    

執行效果圖:

 

\

 

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