Can't set OnCheckedChangeListener to a Checkbox

前端 未结 3 1184
傲寒
傲寒 2021-01-02 01:01

I am trying to set a OnCheckedChangeListener to a CheckBox but my application exits in the run time. I also tried to set listeners for my Tex

相关标签:
3条回答
  • 2021-01-02 01:03

    Implement CheckBox Listener for your class this way, especially if you have more than one CheckBox to deal with, you can handle in switch case blocks and makes your code neater:

     public class MyClass extends AppCompatActivity implements
        CompoundButton.OnCheckedChangeListener,{
    
          CheckBox myCheckBox;
      }
    

    In your onCreate() method put this:

        myCheckBox = (CheckBox)findViewById(R.Id.myCheckBoxName_in_XML_layout);
        mmyCheckBox.setOnCheckedChangeListener(this);
    

    Listern for your checkbox view event like this:

      @Override
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
        switch (buttonView.getId()){
            case R.id.myCheckBoxName_in_XML_layout:
    
                if(isChecked == true) {
                    Toast.makeText(this, "Checked", Toast.LENGTH_SHORT).show();
                } else{
                        Toast.makeText(this, "Unchecked", Toast.LENGTH_SHORT).show();
                       }
    
                break;
             }
    
          }
    
    0 讨论(0)
  • 2021-01-02 01:05

    You can't set the listener for your CheckBox from the ListView like that(it will probably throw a NullPointerException), instead set the listener in the getView() method(you'll also have to keep the CheckBox status so you don't end up with strange rows status). Bellow is an example:

    public class ListViewActivity extends ListActivity {
    
        public class MyCustomAdapter extends ArrayAdapter<String> {
    
            private ArrayList<Boolean> status = new ArrayList<Boolean>();
    
            public MyCustomAdapter(Context context, int textViewResourceId,
                    String[] objects) {
                super(context, textViewResourceId, objects);
                for (int i = 0; i < objects.length; i++) {
                    status.add(false);
                }
            }
    
            @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {
                View row = convertView;
                if (row == null) {
                    LayoutInflater inflater = getLayoutInflater();
                    row = inflater.inflate(R.layout.adapters_listviewactivity_row,
                            parent, false);
                }
    
                TextView label = (TextView) row.findViewById(R.id.weekofday);
                label.setText(month[position]);
                CheckBox checkBox = (CheckBox) row.findViewById(R.id.checkBox);
                checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        Toast.makeText(getApplicationContext(), "" + position,
                                Toast.LENGTH_SHORT).show();
                        if (isChecked) {
                            status.set(position, true);
                        } else {
                            status.set(position, false);
                        }
                    }
                });
                checkBox.setChecked(status.get(position));
                return row;
            }
        }
    
        String[] month = { "January", "February", "March", "April", "May", "June",
                "July", "August", "September", "October", "November", "December" };
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setListAdapter(new MyCustomAdapter(ListViewActivity.this,
                    R.layout.main, month));
        }
    
    }
    

    For the TextView you'll have to do the same thing.

    0 讨论(0)
  • 2021-01-02 01:16
    public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    List<String> mList = new ArrayList<>();
    CheckBox android, java, python, php, unity3D;
    Button submitButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        android = (CheckBox) findViewById(R.id.androidCheckBox);
        android.setOnCheckedChangeListener(this);
        java = (CheckBox) findViewById(R.id.javaCheckBox);
        java.setOnCheckedChangeListener(this);
        python = (CheckBox) findViewById(R.id.pythonCheckBox);
        python.setOnCheckedChangeListener(this);
        php = (CheckBox) findViewById(R.id.phpCheckBox);
        php.setOnCheckedChangeListener(this);
        unity3D = (CheckBox) findViewById(R.id.unityCheckBox);
        unity3D.setOnCheckedChangeListener(this);
    
        submitButton = (Button) findViewById(R.id.submitButton);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("ArrayList Values*******",mList.toString());
            }
        });
    
    }
    
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()) {
            case R.id.androidCheckBox:
                if (android.isChecked()) {
                    mList.add(String.valueOf(android.getText()));
                    Log.e("Android*******",mList.toString());
                }
                else {
                        mList.remove(android.getText());
                }
                break;
    
            case R.id.javaCheckBox:
                if (java.isChecked()) {
                    mList.add(String.valueOf(java.getText()));
                    Log.e("Java*******",mList.toString());
                }
                else {
                    mList.remove(java.getText());
                }
                break;
    
            case R.id.phpCheckBox:
                if (php.isChecked()) {
                    mList.add(String.valueOf(php.getText()));
                    Log.e("PHP*******",mList.toString());
                }
                else {
                    mList.remove(php.getText());
                }
                break;
    
            case R.id.pythonCheckBox:
                if (python.isChecked()){
                    mList.add(String.valueOf(python.getText()));
                    Log.e("Python*******",mList.toString());
                }
                else {
                    mList.remove(python.getText());
                }
                break;
    
            case R.id.unityCheckBox:
                if (unity3D.isChecked()){
                    mList.add(String.valueOf(unity3D.getText()));
                    Log.e("Unity*******",mList.toString());
                }
                else {
                    mList.remove(unity3D.getText());
                }
                break;
          }
      }
    }
    
    0 讨论(0)
提交回复
热议问题