首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓SwitchCompat onCheckedChangeListener无法工作

安卓SwitchCompat onCheckedChangeListener无法工作
EN

Stack Overflow用户
提问于 2015-11-16 22:43:15
回答 1查看 889关注 0票数 4

这是我的代码,它对我不起作用。我使用this示例中的代码。

Xml -

代码语言:javascript
复制
<android.support.v7.widget.SwitchCompat
            android:id="@+id/visibility_switch"
            android:layout_width="wrap_content"
            android:layout_height="55dp"
            android:layout_marginRight="@dimen/mat_card_padding"
            app:showText="false" />

和代码:

代码语言:javascript
复制
mVisibilitySwitchCompat = (SwitchCompat) findViewById(R.id.visibility_switch);
mVisibilitySwitchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {             

                    // this method doesn't work!

                    if (isChecked) {
                        mVisibilityTextView.setText(getString(R.string.visibility_on));
                    } else {
                        mVisibilityTextView.setText(getString(R.string.visibility_off));
                    }
                }
            });
EN

回答 1

Stack Overflow用户

发布于 2015-11-16 22:52:50

尝试使用setTextOff()setTextOn()方法,而不是使用侦听器。当选中状态改变时,开关将自动处理设置正确的状态。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33738016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档