如何实现确认(是/否)DialogPreference?
如何实现显示简单的是/否确认对话框的首选项?
有关示例,请参阅浏览器->设置->清除缓存
。
How can I implement a Preference that displays a simple yes/no confirmation dialog?
For an example, see Browser->Setting->Clear Cache
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个简单的警报对话框,Federico 为您提供了一个可以查找内容的网站。
以下是如何构建警报对话框的简短示例。
That is a simple alert dialog, Federico gave you a site where you can look things up.
Here is a short example of how an alert dialog can be built.
Android 附带了一个内置的 YesNoPreference 类,它完全可以满足您的需求(带有 yes 和 no 选项的确认对话框)。官方源代码参见这里< /a>.
不幸的是,它位于 com.android.internal.preference 包中,这意味着它是 Android 私有 API 的一部分,您无法从应用程序访问它(私有 API 类如有更改,恕不另行通知) ,这就是 Google 不允许您访问它们的原因)。
解决方案:只需从我提供的链接复制/粘贴官方源代码,在应用程序包中重新创建该类即可。我已经尝试过了,效果很好(没有理由不这样做)。
然后,您可以像任何其他首选项一样将其添加到您的
preferences.xml
中。示例:如下所示:
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:
如果您使用首选项 xml 屏幕,或者如果您使用自定义屏幕,则使用 Intent Preference,那么代码如下所示
,然后创建 Activity 类,如下所示,由于不同的人有不同的方法,您可以使用您喜欢的任何方法只是一个例子。
如前所述,有多种方法可以做到这一点。这是您完成任务的方式之一,如果您认为您已经得到了您想要的结果,请接受答案。
Use Intent Preference if you are using preference xml screen or you if you are using you custom screen then the code would be like below
And then Create Activity Class somewhat like below, As different people as different approach you can use any approach you like this is just an example.
As told before there are number of ways doing this. this is one of the way you can do your task, please accept the answer if you feel that you have got it what you wanted.