快速设置重置我的自定义按钮的状态

Quick Settings Resets the State Of My Custom Button

我想知道当您访问快速设置时,该应用程序是否知道或可以被告知。

我有一组按钮用作选项卡(在 Fragment 中,在 CardView) 中,当我下拉快速设置时,按钮失去了它的状态。我试过了有几种机制可以解决,但找不到方法。

按钮使用

.setOnTouchListener(new View.OnTouchListener() { ... }

而不是 onClick listener.

当我尝试在显示对话框之前设置按钮状态时,如果我在

中设置对话框关闭后的状态,我会得到相同的效果
public void onClick(DialogInterface dialog, int which) { ... }

然后我可以获得所需的行为。

我在对话方式方面遗漏了一些知识,因此 quick settings 导致更新基础视图或通知更改。

谁能帮忙。

更新:

按钮xml如下

<Button
        android:id="@+id/fs_btn_tab_month"
        style="@style/DefaultButtonText"
        android:layout_width="match_parent"
        android:layout_height="24dp"
        android:layout_margin="0dp"
        android:background="@drawable/button_default_bg"
        android:gravity="center|center_vertical"
        android:text="@string/fs_btn_tab_month"
        android:textColor="@color/button_text_color" />

和button_default_bg是

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" >
    <shape>
        <solid
            android:color="@color/text_white" />
        <stroke
            android:width="1dp"
            android:color="@color/text_white" />
        <corners
            android:radius="1dp" />

    </shape>
</item>
<item>
    <shape>
        <solid
            android:color="@color/background" />
        <stroke
            android:width="1dp"
            android:color="@color/text_white" />
        <corners
            android:radius="1dp" />

    </shape>
  </item>
</selector>

就 setOnClickListener() 与 setOnTouchListener() 而言,我需要设置按钮的状态,使其看起来像一个选项卡,并且能够从片段中的其他地方执行此操作,但我找不到一种使用 setOnClickListener 的方法。

看起来像这样

当我说失去它的状态时,它会变成蓝色背景色。

我设置"state"如下

mDaySelectedButton.setPressed(false);
mMonthSelectedButton.setPressed(true);
mMonthSelectedButton.setElevation(0f);
mYearSelectedButton.setPressed(false);

一切正常,除了快速设置下拉问题。

问题是,当下拉通知 center/quicksettings 菜单时,按钮失去 "focused" 状态,因为一次只有一个视图可以获得焦点。查看 this Whosebug question 以了解有关不同视图状态的更多详细信息。

如果您想保留按钮的选择,请尝试为此目的使用选定状态。

但是我建议看一下 Android SDK 的 TabLayout, as this encapsulates your desired functionality and would only require some styling to meet your screenshots. There also is a tutorial at codepath.com