未弃用的 findPreference() 方法?- 安卓 [英] Non Deprecated findPreference() Method? - Android

查看:44
本文介绍了未弃用的 findPreference() 方法?- 安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测包含在 ListView 中的 Preference 何时被点击,以便我可以启动一个意图来管理该选择.

我会在我的布局 XML 文件中这样做:

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

我的 java 代码中的以下内容:

Preference myPref = (Preference) findPreference("myKey");myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {公共布尔 onPreferenceClick(偏好偏好){//在这里打开浏览器或意图}});

但是方法 所示)

这将在首选项 UI 中创建一个条目,单击该条目后,将启动具有指定 Intent 的 Activity.

I want to detect when a Preference contained in a ListView gets clicked, so that I can launch an intent to manage that selection.

I would have done like this in my layout XML file:

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

And the following in my java code:

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

But the method public Preference findPreference (CharSequence key) is deprecated.

  1. Is there a non deprecated equivalent?
  2. If not, what if I use it anyway?
  3. How can Fragments help me do my task in a better way?Chek here: Preferences without deprecated methods.


Here you can check the XML layout structure that my activity has, and a snapshot of the application:

XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <Preference 
        android:key="about" 
        android:title="@string/titleAbout" 
        android:summary="@string/summaryAbout" 
    />

    <Preference 
        android:key="labelTaxonomy" 
        android:title="@string/titleLabelTaxonomy" 
        android:summary="@string/summaryLabelTaxonomy" 
    />

</PreferenceScreen>

SNAPSHOT:

After clicking on the About (or Access Label Taxonomy) Preference, I'd like to open an intent of some kind (could also be a video or anything else...the names are misleading).

解决方案

Is there a non deprecated equivalent?

If you are using PreferenceFragment on API Level 11+ devices, you would call findPreference() on it. Otherwise, call findPreference() on your PreferenceActivity, as you have no choice.

If not, what if I use it anyway?

It will work.

How can Fragments help me do my task in a better way?

API Level 11+ introduced PreferenceFragment as another way of constructing the contents of a PreferenceActivity. You are welcome to use them, but if you are still supporting older devices, you cannot use PreferenceFragment for those devices.

That being said:

I want to detect when a Preference contained in a ListView gets clicked, so that I can launch an intent to manage that selection.

You do not need Java code for this. Use:

    <PreferenceScreen
            android:title="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">

        <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />

    </PreferenceScreen>

(as seen in the JavaDocs for PreferenceActivity)

This will create an entry in the preference UI that, when clicked, will start an activity with the specified Intent.

这篇关于未弃用的 findPreference() 方法?- 安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆