Android偏好onclick事件
在我的preferences.xml中,我有一个像这样的首选项元素:
<Preference android:title="About" />
我想分配onClick
事件,因此如果用户单击它,我将能够打开新的Intent
> 或浏览器。我尝试像使用按钮一样进行操作,但这似乎不起作用。
In my preferences.xml I have a preference element like this:
<Preference android:title="About" />
I want to assign onClick
event, so if user would click on it, I would be able to do open new Intent
or browser. I tried to do it like I do with buttons, but this not seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要为该项目设置
android:key
:然后您可以在代码中执行以下操作:
You need to set
android:key
for the item:Then you can do the following in your code:
启动网站:
启动特定活动:
您还可以使用“android:mimetype”来设置mimetype。
to launch a website:
to launch a specific activity:
you can also use "android:mimetype" to set the mimetype.
您需要使用 onPreferenceTreeClick 事件。
例如,请参阅 http://www .javased.com/index.php?source_dir=platform_packages_apps_phone/src/com/android/phone/MobileNetworkSettings.java
You need to use onPreferenceTreeClick event.
For example see http://www.javased.com/index.php?source_dir=platform_packages_apps_phone/src/com/android/phone/MobileNetworkSettings.java
2018+ 更新
今天,
findPreference
方法已被弃用。因此,要实现此目的,只需重写 Preference 片段中的onPreferenceTreeClick
方法即可。例如:此外,如果您需要处理特定首选项元素(例如
ListPreference
)内的点击,您应该在onCreate
中注册setOnPreferenceChangeListener
MySettingsFragment
的方法:2018+ UPDATE
Today, the
findPreference
method is depricated. So, to achieve this, just override theonPreferenceTreeClick
method in your Preference fragment. For example:Furthermore, if you need to handle a click inside particular preference element (e.g.
ListPreference
), you should register thesetOnPreferenceChangeListener
inside theonCreate
method of theMySettingsFragment
:@jason gilbert 的回答的后续内容
我在 targetSdkVersion 25 上,他的回答不起作用,我不得不用 Preference 标签包装意图标签。例子:
A followup on @jason gilbert's answer
I'm on targetSdkVersion 25, and his answer didn't work, I had to wrap the intent tag with a Preference tag. Example: