attach onClickListener to ToggleButton

浪子不回头ぞ 提交于 2019-12-23 06:46:00

问题


I have a ToggleButton and I need to set up simple click actions. How do I implement a simple click listener for a ToggleButton?

If you need details please ask.


回答1:


ToggleButton extends View, so you can simply use View.setOnClickListener(), like this:

// get your ToggleButton
ToggleButton b = (ToggleButton) findViewById(R.id.myButton);

// attach an OnClickListener
b.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // your click actions go here
    }
});



回答2:


    this.someToggleButton = (ToggleButton)findViewById(R.id.someToggleButton) ;
    this.someToggleButton.setOnCheckedChangeListener( new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
            doSomethingWith(toggleButton, isChecked) ;
        }
    }) ;



回答3:


Use View.setOnClickListener() and Check state of button.

    final ToggleButton tB = (ToggleButton) findViewById(R.id.toggleButton1);
    tB.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(tB.isChecked()){
                //Button is ON
                            // Do Something 
            }
            else
            //Button is OFF
                            // Do Something     
        }
    });



回答4:


Just to add a point not emphasised in the other answers - programatically binding a click handler is a bit heavy on the bolierplate code. As mentioned in the docs, it's only necessary in certain scenarios, such as:

  • If the ToggleButton is instantiated at runtime
  • If the click behaviour is defined in a Fragment subclass

If the ToggleButton is defined in the layout, it's far simpler and cleaner to bind a handler method there

<ToggleButton 
  android:id="@+id/togglebutton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textOn="On"
  android:textOff="Off"
  android:onClick="onToggleClicked"/>

Then only the handler method needs to be defined in the Activity Java

public void onToggleClicked(View view) {
    if(((ToggleButton) view).isChecked()) {
        // handle toggle on
    } else {
       // handle toggle off
    }    
}

Note the method can have any name, but the signature must meet these criteria:

  • Must be a public method
  • Must return void
  • Must take a single argument of type View (this will be the View which was clicked)



回答5:


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
        Toast.makeText(getApplicationContext(),"on",Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(getApplicationContext(),"off",Toast.LENGTH_SHORT).show();
    }
}



回答6:


To add it from the code, you can do something like:

yourButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
      finish();
    }
  });

However, you can also specify in the XML for your button, which method you want to be associated with the onClick action/event.




回答7:


mTB = (ToggleButton) findViewById(R.id.toggleButton1);
mTB.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // Is the toggle on?
            boolean on = ((ToggleButton) v).isChecked();

            if (on) {
                // Enable here
            } else {
                // Disable here
            }

        }
    });



回答8:


if above codes don't work try

b.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
       // your click actions go here
    }
});



回答9:


Try setOnCheckedChangeListener method of your ToggleButton class

ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked) {
        // 
    }
}) ;


来源:https://stackoverflow.com/questions/7141531/attach-onclicklistener-to-togglebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!