Android:复选框监听器

时间:2011-12-05 14:31:32

标签: java android android-checkbox

我想将监听器放在CheckBox上。我找了信息,就像这样:

satView = (CheckBox)findViewById(R.id.sateliteCheckBox);

satView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (isChecked){
            // perform logic
        }
    }

});

问题是Eclipse认为它是OnCheckedChangeListener的{​​{1}}并且它不起作用。我该如何解决这个问题?

10 个答案:

答案 0 :(得分:224)

你可以这样做:

satView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

       }
   }
);     

答案 1 :(得分:45)

你也可以选择一个简单的View.OnClickListener

satView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if(((CompoundButton) view).isChecked()){
            System.out.println("Checked");
        } else {
            System.out.println("Un-Checked");
        }
    }
});

答案 2 :(得分:18)

您收到错误是因为导入了错误的包。您应该import android.widget.CompoundButton.OnCheckedChangeListener;

所以回调应该是:

        box.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

        }
    });

答案 3 :(得分:8)

试试这个:

satView = (CheckBox) findViewById(R.id.sateliteCheckBox);

satView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          if (buttonView.isChecked()) { 
                // checked
          } 
          else 
          {
                // not checked
          }
    }

});

希望这有帮助。

答案 4 :(得分:4)

使用RadioGroup group更改CompoundButton buttonView,然后按Ctrl + Shift + O修复导入。

答案 5 :(得分:4)

您可以使用此代码。

public class MySampleActivity extends Activity {
    CheckBox cb1, cb2, cb3, cb4;
    LinearLayout l1, l2, l3, l4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb3 = (CheckBox) findViewById(R.id.cb3);
        cb4 = (CheckBox) findViewById(R.id.cb4);
        l1 = (LinearLayout) findViewById(R.id.l1);
        l2 = (LinearLayout) findViewById(R.id.l2);
        l3 = (LinearLayout) findViewById(R.id.l3);
        l4 = (LinearLayout) findViewById(R.id.l4);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(1));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(2));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(3));
        cb1.setOnCheckedChangeListener(new MyCheckedChangeListener(4));
    }

    public class MyCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {
        int position;

        public MyCheckedChangeListener(int position) {
            this.position = position;
        }

        private void changeVisibility(LinearLayout layout, boolean isChecked) {
            if (isChecked) {
                l1.setVisibility(View.VISIBLE);
            } else {
                l1.setVisibility(View.GONE);
            }

        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (position) {
                case 1:
                    changeVisibility(l1, isChecked);
                    break;
                case 2:
                    changeVisibility(l2, isChecked);
                    break;
                case 3:
                    changeVisibility(l3, isChecked);
                    break;
                case 4:
                    changeVisibility(l4, isChecked);
                    break;
            }
        }
    }
}

答案 6 :(得分:3)

试试这个

satView.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (isChecked){
                // perform logic
            }
        }

    });

答案 7 :(得分:2)

h.chk.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        CheckBox chk=(CheckBox)view; // important line and code work
        if(chk.isChecked())
        {
            Message.message(a,"Clicked at"+position);
        }
        else
        {
            Message.message(a,"UnClick");
        }
    }
});

答案 8 :(得分:0)

Chris将接受的答案翻译成Kotlin:

val checkBox: CheckBox = findViewById(R.id.chk)
checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
    // Code here 
}

答案 9 :(得分:0)

如果您希望通过接口实现在Kotlin中进行此操作。

ls -al /
drwxr-xr-x   6 root  admin   192 10 26  2018 Users

ls -ale@ /Users
drwxr-xr-x+ 57 jokerham  staff  1824  7  4 23:10 jokerham
0: group:everyone deny delete

ls -ale@ /Users/jokerham
drwxr-xr-x+  29 jokerham  staff      928  3 24 18:03 Documents
0: group:everyone deny delete

ls -ale@ /Users/jokerham/Documents
drwxr-xr-x  11 jokerham  staff      352  6 21 20:44 Workspace

ls -ale@ /Users/jokerham/Documents/Workspace
drwxr-xr-x   6 jokerham  staff    192  7  4 22:51 web

ls -ale@ /Users/jokerham/Documents/Workspace/web
drwxr-xr-x   9 jokerham  staff   288  7  5 07:50 Nanitelink

ls -ale@ /Users/jokerham/Documents/Workspace/web/Nanitelink
drwxr-xr-x   6 jokerham  admin    192  6 28 07:17 www

ls -ale@ /Users/jokerham/Documents/Workspace/web/Nanitelink/www
-rw-r--r--  1 jokerham  admin   173  6 21 21:38 .htaccess