EditText/RadioButton/CheckBox使用
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package com.test.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class HelloAndroid extends Activity {
private EditText _editText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_editText = (EditText)findViewById(R.id.EditText01);
_editText.setHint("请输入一个值");
_editText.setOnKeyListener(new EditText.OnKeyListener(){
public boolean onKey(View arg0, int arg1, KeyEvent arg2){
dispToast("输入值:"+_editText.getText().toString());
return false;
}
});
RadioGroup rgp = (RadioGroup)findViewById(R.id.RadioGroup01);
rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId){
dispToast(Integer.toString(checkedId));
}
});
CheckBox cb1 = (CheckBox)findViewById(R.id.CheckBox01);
cb1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (isChecked){
dispToast("选择了"+buttonView.getText().toString());
}
}
});
}
public void dispToast(String str){
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
public class HelloAndroid extends Activity {
private EditText _editText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_editText = (EditText)findViewById(R.id.EditText01);
_editText.setHint("请输入一个值");
_editText.setOnKeyListener(new EditText.OnKeyListener(){
public boolean onKey(View arg0, int arg1, KeyEvent arg2){
dispToast("输入值:"+_editText.getText().toString());
return false;
}
});
RadioGroup rgp = (RadioGroup)findViewById(R.id.RadioGroup01);
rgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
public void onCheckedChanged(RadioGroup group, int checkedId){
dispToast(Integer.toString(checkedId));
}
});
CheckBox cb1 = (CheckBox)findViewById(R.id.CheckBox01);
cb1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if (isChecked){
dispToast("选择了"+buttonView.getText().toString());
}
}
});
}
public void dispToast(String str){
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}