0

我在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;
    }

}
4

2 回答 2

1

您不应该使用 checkChanged 侦听器,除非您希望在用户选中/取消选中复选框时发生某些事情。由于您希望仅在单击按钮时才发生操作,因此您只需要 onClickListener 并且它应该您的 onCheckChangedListener 之外

试试这样:

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) {
                // You don't need to use onCheckChangeListener.
            }
        });

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(c1.isChecked()){
                    Toast.makeText(getApplicationContext(), "i accept", 1).show();
                }else{
                    Toast.makeText(getApplicationContext(), "i don't accept", 1).show();
                }
            }
        });
    }//end onCreate
}//end MainActivity
于 2013-05-01T14:43:08.977 回答
0

在 Button Click 内移动c1.isChecked()以检查 CheckBox 是否在 Button click 上被选中,然后根据 CheckBox 状态显示 toast 消息。将您的代码更改为:

b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(c1.isChecked()){  //<<< check here if checkbox clicked or not
                    Toast.makeText(getApplicationContext(), 
                                             "i accept", 1).show();
                }else{
                    Toast.makeText(getApplicationContext(), 
                                       "i don't accept", 1).show();
                }
            }
        });
于 2013-05-01T14:47:41.863 回答