正常工作,我已經在Android的做了一個簡單checkbo [X演示程序,因爲我已經把chekbox和button.I希望的是,當按下chekbox並按下按鈕泰德表示乾杯「我接受」,否則當產生按下按鈕舉杯爲‘我不接受’...我造的,但是問題是,當我未選中的chekbox..it直接顯示吐司(無需點擊按鈕);複選框不是在Android的
mainActivity.java
package com.example.chechboxdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
CheckBox c1;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1=(CheckBox)findViewById(R.id.checkBox1);
b=(Button)findViewById(R.id.button1);
c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(c1.isChecked()){
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "i accept", 1).show();
}
});
}
else{
Toast.makeText(getApplicationContext(), "i don't accept", 1).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
您已設置按鈕的''onClick'複選框B'裏面'onCheckedChanged',儘量外面放置 – Pragnani 2013-05-01 14:41:15
讓我知道什麼是你想實現 – Pragnani 2013-05-01 14:41:32