简体   繁体   English

如何获取ToggleButton来识别Android活动中的多个单击?

[英]How do I get a ToggleButton to recognize more than one click in an Android activity?

I'm trying to get a ToggleButton to switch the text in a TextView back and forth between two types of text. 我正在尝试使用ToggleButton在两种类型的文本之间来回切换TextView中的文本。 When I click the first time, it switches the default text to the alternate text but will not switch it back when I click it again. 第一次单击时,它将默认文本切换为替代文本,但是当我再次单击它时,不会将其切换回原来的文本。 Here is my code: 这是我的代码:

ELswitch = (ToggleButton)findViewById(R.id.toggleButton1);
    ELswitch.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(ELswitch.isEnabled()==true)
            {
                InputStream iFile = getResources().openRawResource(R.raw.raw_text_file_alternate);
                try {

                    text.setText(inputStreamToString(iFile));
                    text.setFocusable(false);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } else {
                InputStream iFile = getResources().openRawResource(R.raw.raw_text_file_default);
                try {

                    text.setText(inputStreamToString(iFile));
                    text.setFocusable(false);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    });

One suggestion I received to eliminate the problem was just to use a regular button and a counter and mod by 2. It seems like there should be a way to do this from a ToggleButton though. 我收到的消除此问题的建议是仅使用常规按钮和计数器,并按2进行调制。似乎应该有一种方法可以通过ToggleButton进行。 I think I might be instantiating the ToggleButton incorrectly or using the if-statements incorrectly but I can't find the error. 我想我可能错误地实例化了ToggleButton或错误地使用了if语句,但是我找不到错误。 Any ideas? 有任何想法吗?

It looks like your comparison is for: 看起来您的比较适用于:

ELswitch.isEnabled()==true

Which would just tell you that the switch is enabled or not, not whether or not it is checked. 这只会告诉您该开关已启用,而不是是否已选中。 You may want to change that to check if it is checked: 您可能需要更改它以检查是否已选中:

ELswitch.isChecked()==true

You can find more information about the Toggle button in the following guide/documentation: 您可以在以下指南/文档中找到有关“切换”按钮的更多信息:

http://developer.android.com/reference/android/widget/ToggleButton.html http://developer.android.com/reference/android/widget/ToggleButton.html

From your question, it also seems that you may want to use an OnCheckedChangeListener instead of your OnClickListener . 从您的问题来看,您似乎还想使用OnCheckedChangeListener而不是OnClickListener As example for this can be found in the guide below: 例如,可以在下面的指南中找到:

http://developer.android.com/guide/topics/ui/controls/togglebutton.html http://developer.android.com/guide/topics/ui/controls/togglebutton.html

Hopefully that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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