get state of ToggleButton through handler

痴心易碎 提交于 2019-12-01 13:49:20

问题


I need to be able to access the state of a ToggleButton seeing is how there is no way to create methods for the specific state of a ToggleButton. So here is where I'm at so far:

ToggleButton syncSwitch = (ToggleButton)findViewById(R.id.toggleButton1);
syncSwitch.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View view){
        Toast.makeText(getApplicationContext(), "Click!", Toast.LENGTH_SHORT).show();
    }
});

Now all I need is some type of boolean method that can tell the handler the state of the ToggleButton.


回答1:


What do you mean by 'create methods for the specific state'? You can get the state using isChecked().




回答2:


You can also check by using togglebtn.setOnCheckedChangeListener Listener like this:

ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(getApplicationContext(),
                        String.valueOf(buttonView.isChecked()), Toast.LENGTH_SHORT).show();
            }
        });


来源:https://stackoverflow.com/questions/7142543/get-state-of-togglebutton-through-handler

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