gpt4 book ai didi

android - OnCheckedChangeListener 未立即调用

转载 作者:行者123 更新时间:2023-11-30 01:56:29 26 4
gpt4 key购买 nike

我在菜单中有一个开关,它在操作栏上显示为一个操作。当我将 checked 属性设置为 true 后,不会立即调用 onCheckedChangeListener。该开关确实显示在操作栏中,每次按下它时都会调用 checkedchangelistener。但是,它并没有在开始时应该被调用。这怎么可能?为什么不调用它?我该如何解决这个问题?

这是onCreateMenuOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.i(TAG, "onCreateOptionsMenu()");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);

serviceSwitch = (Switch) menu.findItem(R.id.Switch).getActionView().findViewById(R.id.switchid);
serviceSwitch.setOnCheckedChangeListener(mCheckedChangeListener);
serviceSwitch.setChecked(false);

//OnCheckedChange is not being called here. I have set a breakpoint at the method.
//I have also looked at the log - nothing.
//It is being called when I click the switch however.

return true;
}

private OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

Log.i(TAG, isChecked ? "true" : "false";

}
}

这是 xml:

R.menu.mainmenu

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title=""
android:id="@+id/Switch"
android:actionLayout="@layout/switch_layout"
android:showAsAction="always"></item>
</menu>

R.layout.switch_layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<Switch
android:id="@+id/switchid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:textOff="Off"
android:textOn="On"
android:checked="true"/>

</RelativeLayout>

最佳答案

R.layout.switch_layout

布局文件中的 Switch checked 值已经为真。

android:checked="true"

因此,当将其值设置为 true 时,不会调用其已检查的更改监听器。

希望你明白我的意思。

关于android - OnCheckedChangeListener 未立即调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32129037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com