安卓preferences onclick事件事件、preferences、onclick

2023-09-04 08:25:37 作者:我很怪但我不坏

在我的preferences.xml我有这样的preference元素:

In my preferences.xml I have a preference element like this:

<Preference android:title="About" />

我要分配的onClick 事件,因此,如果用户将点击它,我就可以做开放新的意图或浏览器。我试图做到这一点像我这样做的按钮,但是这似乎并没有工作。

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.

推荐答案

巴德尔

您需要设置安卓键的项目,然后在你的code,你可以做...

You need to set android:key for the item, Then in your code you can do...

假设你使用的XML如下:

Assuming you use the following in your XML:

<Preference android:title="About" android:key="myKey"></Preference>

然后,你可以做你的code以下内容:

Then you can do the following in your code:

Preference myPref = (Preference) findPreference("myKey");
myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
             public boolean onPreferenceClick(Preference preference) {
                 //open browser or intent here
             }
         });