各种按钮按下并显示消息

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");
}
});
    }

 
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");
     }
    }
   }
  });
    }
 
 
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();
    }
   }
  });
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值