选择首选项后如何启动浏览器
我正在创建一个首选项菜单,并希望在单击特定首选项时启动浏览器(具有特定的 url)。我知道这是可以做到的,但我现在似乎无法让它发挥作用。
有什么想法吗?
谢谢
######SOLUTION所以在我的脑屁消失后,这就是我所做的:
getPreferenceManager()
.findPreference("my_preference_key")
.setOnPreferenceClickListener(
new Preferences.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://some_url_here"));
startActivity(intent);
return true;
}
});
I am creating a preference menu and would like to launch a browser (with a specific url) when a particular preference is clicked on. I know this can be done, but I cant seem to get it to work right now.
Any ideas?
Thanks
######SOLUTION
So after my brain fart disappeared, this is what i did:
getPreferenceManager()
.findPreference("my_preference_key")
.setOnPreferenceClickListener(
new Preferences.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://some_url_here"));
startActivity(intent);
return true;
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您已经有一个
PreferenceFragment
或PreferenceActivity
以及加载屏幕的一行:无需编写任何额外内容即可创建网站链接(以及更多)代码!以下演示了我无需编写一行 Java 代码即可实现的功能(“链接”部分):
启动网站是最简单的。请注意,这些首选项都没有任何键,因此即使我愿意,也无法通过代码访问它们。当然,缺少密钥完全是可选的。
src/main/res/xml/my_prefs.xml
注意:与任何意图一样,它们可能无法解析,因此最好禁用/隐藏按钮,此类解决了这个问题:
用法完全相同:
Assuming you already have a
PreferenceFragment
orPreferenceActivity
in place with a line that loads the screen:It's possible to do website links (and many more) without writing any additional code! Here is a demonstration of the capabilities I was able to achieve without writing a single line of Java code ("Links" section):
Launching a website is the simplest one. Notice that none of these preferences have any keys, so they're not accessible from code even if I wanted to. Of course this lack of key is entirely optional.
src/main/res/xml/my_prefs.xml
Note: as with any intents, they may not resolve, so it's better to disable/hide the button, this class solves that:
usage is exactly the same:
可以简化为
Can be reduced to