How to implement a confirmation (yes/no) DialogPreference?

后端 未结 3 1260
后悔当初
后悔当初 2020-12-04 09:59

How can I implement a Preference that displays a simple yes/no confirmation dialog?

For an example, see Browser->Setting->Clear Cache.

3条回答
  •  无人及你
    2020-12-04 10:48

    Android comes with a built-in YesNoPreference class that does exactly what you want (a confirm dialog with yes and no options). See the official source code here.

    Unfortunately, it is in the com.android.internal.preference package, which means it is a part of Android's private APIs and you cannot access it from your application (private API classes are subject to change without notice, hence the reason why Google does not let you access them).

    Solution: just re-create the class in your application's package by copy/pasting the official source code from the link I provided. I've tried this, and it works fine (there's no reason why it shouldn't).

    You can then add it to your preferences.xml like any other Preference. Example:

    
    

    Which looks like this:

    screenshot

提交回复
热议问题