简体   繁体   中英

Put check box listener into button listener

I have a save button and checkbox in Activtiy A .

When save button is clicked, it will display the checkbox text if checkbox is checked .

         save.setOnClickListener(new View.OnClickListener() { // if save button clicked

                         @Override
                         public void onClick(View v) {
                                 if(checkbox.isChecked()) {
                                     returnIntent.putExtra("outstation", checkbox.getText().toString());
                                     Toast.makeText(getApplicationContext(),checkbox.getText().toString(),Toast.LENGTH_LONG).show();
                                 }
                              }
                          });

     public void addListenerOnChk() // for checkbox
        {
            checkbox=(CheckBox)findViewById(R.id.checkBox);

            checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                        if(((CheckBox)v).isChecked())
                        {
                              // Toast.makeText(getApplicationContext(),checkbox.getText().toString(),Toast.LENGTH_LONG).show();
                        }
                    }

                });
            }

The above code works fine but it looked weird. I define addListenerOnChk() in OnCreate method. How can I put the check box inside save onClick instead of create two seperate OnClick ? (One is save button and another is checkbox)

The first method is correct..

The second method is not necessary. The checkBox.isChecked is everything you need to use.. you don't have to set the listener also for the checkbox.

save.setOnClickListener(new View.OnClickListener() { // if save button clicked

  @Override
  public void onClick(View v) {
 if(checkbox.isChecked()) {
returnIntent.putExtra("outstation", checkbox.getText().toString());
Toast.makeText(getApplicationContext(),checkbox.getText().toString(),Toast.LENGTH_LONG).show();
checkbox.setText("your text here");
      } else {
checkbox.setText("");
}
   }
});

Hope this helps

Referring to this question

"How can I put the check box inside save onClick instead of create two seperate OnClick ?"

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //your UI setup and findViewById stuff

        save.setOnClickListener(this);//implement - View.OnClickListener
        checkBox.setOnClickListener(this);//implement - View.OnClickListener
        checkBox.setOnCheckedChangeListener(this);//prefer this one get check box status

    }


    @Override
    public void onClick(View v) {

        if(v.getId()==R.id.save_button){
            //save button action stuff
        } else if(v.getId()==R.id.check_box){
            //checkbox  button action stuff
            Log.i("TAG", " Check box status " + checkBox.isChecked());
        }

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.i("TAG", " Check box status " + isChecked);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM