1、Button
public void buttonClicked(){
//按钮按下消息窗弹出成功
setContentView(R.layout.activity_button_clicked_android_main);
final Button basic_button = (Button) findViewById(R.id.button1);
basic_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(ButtonClickedAndroidMainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
}
2、CheckedBox
public void checkBoxClicked(){
final CheckBox check_button = (CheckBox) findViewById(R.id.checkBox1);
check_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.checkBox1);
tv.setText(check_button.isChecked() ?
"This option is checked" :
"This option is not checked");
}
});
}
final CheckBox check_button = (CheckBox) findViewById(R.id.checkBox1);
check_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) findViewById(R.id.checkBox1);
tv.setText(check_button.isChecked() ?
"This option is checked" :
"This option is not checked");
}
});
}
3、radioButton:
public void radioButton() {
final RadioGroup group = (RadioGroup)findViewById(R.id.radioGroup1);
final TextView tv = (TextView)findViewById(R.id.textView1);
group.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId != -1){
RadioButton rb = (RadioButton) findViewById(checkedId);
if(rb != null){
tv.setText("You choose: " + rb.getText());
}else {
tv.setText("Choose 2");
}
}
}
});
}
final RadioGroup group = (RadioGroup)findViewById(R.id.radioGroup1);
final TextView tv = (TextView)findViewById(R.id.textView1);
group.setOnCheckedChangeListener(new
RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(checkedId != -1){
RadioButton rb = (RadioButton) findViewById(checkedId);
if(rb != null){
tv.setText("You choose: " + rb.getText());
}else {
tv.setText("Choose 2");
}
}
}
});
}
4、清除选择
public void clearRadioButton(){
final Button clear_choice = (Button) findViewById(R.id.button2);
clear_choice.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
if(group != null){
group.clearCheck();
}
}
});
}
final Button clear_choice = (Button) findViewById(R.id.button2);
clear_choice.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
RadioGroup group = (RadioGroup) findViewById(R.id.radioGroup1);
if(group != null){
group.clearCheck();
}
}
});
}