我在android中制作了一个简单的checkbo [x演示程序,因为我放了一个checkbox和一个按钮。我想要的是当按下chekbox并按下按钮时,吐司显示“我接受”,否则当按下按钮吐司生成作为“我不接受”......我做到了,但问题是当我取消选中复选框时......它直接显示吐司(不点击按钮);
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;
}
}