com.gc.materialdesign.views.Switch不首先调用OnClickListener单击

时间:2018-03-12 03:39:58

标签: android

我正在尝试让Switch(来自https://github.com/navasmdc/MaterialDesignLibrary)注册第一个切换。情况是,当我第一次启动应用程序,并将Switch从OFF切换到ON,而不是将屏幕变为灰度时,没有任何反应。我相信这是由于代码行materialdesign:check="true",应用程序认为Switch已经打开,因此没有状态更改。但是,如果我的屏幕已经是灰度,则第一次从ON切换到OFF会进行注册。在那之后切换工作完全没问题。

我真的很困惑为什么会这样。

  

activity_main.xml中

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.jeremy.monochrometoggler.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:layout_gravity="center_horizontal"
        android:text="Toggle monochrome" />

    <com.gc.materialdesign.views.Switch
        android:id="@+id/monochromeSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#1E88E5"
        android:layout_gravity="center_horizontal"
        materialdesign:check="true" />

</LinearLayout>
  

MainActivity.java

    private void initMonochromeSwitch(final Switch monochromeSwitch) {
    if (Settings.Secure.getInt(getContentResolver(), ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, 0) == 0) {
        monochromeSwitch.setChecked(false);
    } else {
        monochromeSwitch.setChecked(true);
    }

    monochromeSwitch.setOncheckListener(new Switch.OnCheckListener() {
        @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void onCheck(Switch view, boolean check) {
            //Checks if this app can modify system settings
            boolean canWriteSettings = getApplicationContext().checkCallingOrSelfPermission("android.permission.WRITE_SECURE_SETTINGS") == PackageManager.PERMISSION_GRANTED;

            if (canWriteSettings) {
                if (check) {
                    toggleMonochrome(1, getApplicationContext().getContentResolver());
                } else {
                    toggleMonochrome(0, getApplicationContext().getContentResolver());
                }
            } else {
                //If currently cant modify system settings, app will ask for permission
                showRootWorkaroundInstructions(getApplicationContext());
                monochromeSwitch.setChecked(false);
            }
        }
    });
}

public static void toggleMonochrome(int value, ContentResolver contentResolver) {
    Settings.Secure.putInt(contentResolver, ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED, value);
    if (value == 0) {
        Settings.Secure.putInt(contentResolver, ACCESSIBILITY_DISPLAY_DALTONIZER, -1);
    } else if (value == 1) {
        Settings.Secure.putInt(contentResolver, ACCESSIBILITY_DISPLAY_DALTONIZER, 0);
    }
}
  

logcat的

第一次切换 - 没有任何事情发生

03-12 14:30:50.882 32598-32598/com.example.jeremy.monochrometoggler D/ViewRootImpl@cc84c90[MainActivity]: ViewPostImeInputStage processPointer 0
03-12 14:30:50.884 32598-32598/com.example.jeremy.monochrometoggler W/System: ClassLoader referenced unknown path: /system/framework/QPerformance.jar
03-12 14:30:50.886 32598-32598/com.example.jeremy.monochrometoggler E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
03-12 14:30:50.886 32598-32598/com.example.jeremy.monochrometoggler V/BoostFramework: BoostFramework() : mPerf = null
03-12 14:30:51.099 32598-32598/com.example.jeremy.monochrometoggler D/ViewRootImpl@cc84c90[MainActivity]: ViewPostImeInputStage processPointer 1

2nd Toggle - 正确切换回颜色(我在onClick Listener中使用toast进行了测试)

03-12 14:31:40.754 32598-32598/com.example.jeremy.monochrometoggler D/ViewRootImpl@cc84c90[MainActivity]: ViewPostImeInputStage processPointer 0
03-12 14:31:40.903 32598-32598/com.example.jeremy.monochrometoggler D/ViewRootImpl@cc84c90[MainActivity]: ViewPostImeInputStage processPointer 1

第3次切换 - 正确更改为greyScale

03-12 14:32:00.242 32598-32598/com.example.jeremy.monochrometoggler D/ViewRootImpl@cc84c90[MainActivity]: ViewPostImeInputStage processPointer 0
03-12 14:32:00.491 32598-32598/com.example.jeremy.monochrometoggler D/ViewRootImpl@cc84c90[MainActivity]: ViewPostImeInputStage processPointer 1

0 个答案:

没有答案