Adroid中Check和RedioButton的应用

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {
	//Ctrl +t    或者 F4 看父类关系图 
	CheckBox mCheckBox1,mCheckBox2;
	RadioGroup mRadioGroup1,mRadioGroup2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initFind();
		//清楚第二组的默认选项
		mRadioGroup2.clearCheck();
		
		initCheckEvent();
		
		initRadioEvent();
	}

	private void initRadioEvent() {
		// TODO Auto-generated method stub
		//RadioGroup.OnCheckedChangeListener 
		mRadioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			//group:哪个组的
			//checkedId:组group内的哪个RadioButton被选中 
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				/*switch (key) {
				case value:
					
					break;

				default:
					break;
				}*/
				RadioButton radioButton=(RadioButton) findViewById(checkedId);
				String text1=radioButton.getText().toString();
				String text2="";
				//读第二组
				int id2=mRadioGroup2.getCheckedRadioButtonId();
				if(id2 != -1){
					RadioButton radioButton2=(RadioButton) findViewById(id2);
					text2="radio2:Id="+id2+",text2="+radioButton2.getText();
					
				}
				Toast.makeText(
						MainActivity.this,
						"radioButton=>checkedId="+checkedId+"text1="+text1+text2,
						Toast.LENGTH_LONG).show();

			}
		});
	}

	private void initCheckEvent() {
		// TODO Auto-generated method stub
		//CheckBox =>setOnCheckedChangeListener=>CompoundButton.OnCheckedChangeListener;
		
		mCheckBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			// buttonView 哪个组件的事件,
			// isChecked 勾选状态   
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				//Toast 弹出一个提示消息框
				//makeText(context, text, duration) 
				// context 上下文 
				//  text 你需要显示的内容  
				//duration 显示的时间长短 ,常量 
				// 返回内容 Toast			
				Boolean check2=mCheckBox2.isChecked();
				mCheckBox2.setChecked(!isChecked);
				Toast.makeText(
						MainActivity.this,
						"onCheckedChanged:isChecked="+isChecked+",check2="+check2,
						Toast.LENGTH_LONG).show();
			}
		});
	}

	private void initFind() {
		// TODO Auto-generated method stub
		mCheckBox1=(CheckBox) findViewById(R.id.checkBox1);
		mCheckBox2=(CheckBox) findViewById(R.id.checkBox2);
		mRadioGroup1=(RadioGroup) findViewById(R.id.radioGroup1);
		mRadioGroup2=(RadioGroup) findViewById(R.id.radioGroup2);
	}

	@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;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}


你可能感兴趣的:(checkbox,RedioButton)