0

我有一个具有多个 SwitchpreferenceCompat 的首选项屏幕。我需要在 settingActivity 中让用户选择主题变化、振动等。任何人都可以通过单击设置活动中的开关来帮助我设置振动或主题更改。我在我的 settingActivity 片段中使用了它,但是当我单击设置活动时,应用程序停止

SwitchPreferenceCompat switchPref = findPreference("key");
switchPref.setChecked(true);

`res/xml/root_preference.xml

 <SwitchPreferenceCompat
        android:id="@+id/vibrate"
        app:key="On"
        app:title="Vibrate"
        android:summaryOff="Off"
        android:summaryOn="On"
        app:icon="@drawable/ic_baseline_vibration_24"
        android:defaultValue="true" />

    <SwitchPreferenceCompat
        app:key="On"
        app:title="Sound"
        app:summaryOff="Off"
        app:summaryOn="On"
        app:icon="@drawable/ic_baseline_volume_up_24"
        android:defaultValue="true"/>

</PreferenceCategory>

用户单击应用程序上的振动必须在活动中单击按钮时振动。我怎样才能做到这一点?

在此处输入图像描述

4

1 回答 1

0

使用 Androidx 库

implementation 'androidx.preference:preference:1.1.1'

然后创建xml文件夹并在下面添加您的SwitchPreferenceCompat喜欢:

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

<SwitchPreferenceCompat
    android:id="@+id/vibrate"
    android:key="vibrate"
    android:summary="It will vibrate your phone"
    android:title="Vibrate"/>

<SwitchPreferenceCompat
    android:id="@+id/changeTheme"
    android:key="theme"
    android:title="Change Theme"
    android:summary="It will toggle your app theme"/>

</androidx.preference.PreferenceScreen>

然后创建SwitchPreferenceCompatActivity如下:

public class SwitchPreferenceCompatActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_switch_preference_compat);

    if(savedInstanceState == null) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.preferences_container, new MySettingsFragment())
                .commit();
    }


}

public static class MySettingsFragment extends PreferenceFragmentCompat {
    SharedPreferences.OnSharedPreferenceChangeListener listener;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.my_switch_preference_compat, rootKey);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
                Log.e("key", key);
                if(key.equals("vibrate") && sharedPreferences.getBoolean("vibrate", false)) {
                    vibratePhone();
                }
            }
        };

    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(listener);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(listener);
    }

    private void vibratePhone() {
        Vibrator vibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
        if (vibrator != null) {
    // Vibrate for 500 milliseconds
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                vibrator.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE));
            } else {
                //deprecated in API 26
                vibrator.vibrate(500);
            }
        }
    }
}

}

注意:添加<uses-permission android:name="android.permission.VIBRATE" />清单文件的权限。

于 2020-10-04T11:17:42.617 回答