Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

java - Non Deprecated findPreference() Method? - Android

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:

snapshopt

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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...