非德precated发现preference()方法? - 机器人 [英] Non Deprecated findPreference() Method? - Android
问题描述
我想检测时, preference
包含在的ListView
被点击,那这样我就可以推出的意图来管理的选择。
我会在我的布局做过这样的 XML
文件:
< preference机器人:标题=关于安卓键=的myKey>< / preference>
在我的的Java
code以下内容:
preference我的preF =(preference)找到preference(的myKey);
我的pref.setOn preferenceClickListener(新在preferenceClickListener(){
公共布尔在preferenceClick(preference preference){
//打开浏览器或意图在这里
}
});
但该方法<一href="http://developer.android.com/reference/android/$p$pference/$p$pferenceGroup.html#find$p$pference%28java.lang.CharSequence%29"><$c$c>public preference发现preference(CharSequence的键) 是德precated。
- 是否有非去precated等同?
- 如果不是,如果我反正又有什么用呢? ?
- 如何
片段
帮我做我的任务以更好的方式仄这里:<一href="http://stackoverflow.com/questions/6503496/$p$pferences-without-de$p$pcated-methods">$p$pferences无德precated方法的。
在这里,您可以检查 XML
布局结构,我的活动有,而应用程序的快照:
XML:
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT; preferenceScreen的xmlns:机器人=http://schemas.android.com/apk/res/android&GT;
&LT; preference
机器人:关键=约
机器人:标题=@字符串/ titleAbout
机器人:总结=@字符串/ summaryAbout
/&GT;
&LT; preference
机器人:关键=labelTaxonomy
机器人:标题=@字符串/ titleLabelTaxonomy
机器人:总结=@字符串/ summaryLabelTaxonomy
/&GT;
&LT; / preferenceScreen&GT;
快照:
点击关于(或Access标签分类)后 preference
,我想开一个意图
(也可以是视频或其他任何...的名称误导)。
有没有非去precated等同?
如果您在API级别11+设备使用 preferenceFragment
,你会叫找到preference()
就可以了。否则,调用找到preference()
在 preferenceActivity
,因为你别无选择。
如果不是,如果我无论如何使用它吗?
这会工作。
如何才能片段帮我做我的任务以更好的方式?
API等级11+出台 preferenceFragment
作为构建的内容的另一种方式 preferenceActivity
。欢迎您使用它们,但如果你仍然支持旧设备,则不能使用 preferenceFragment
对于这些设备。
话虽这么说:
我想检测时,包含在一个ListView一个preference被点击,这样我就可以启动一个意图来管理的选择。
您不需要的Java code这一点。用途:
&LT; preferenceScreen
机器人:标题=@字符串/ title_intent_ preference
机器人:总结=@字符串/ summary_intent_ preference&GT;
&LT;意图安卓行动=android.intent.action.VIEW
机器人:数据=http://www.android.com/&GT;
&LT; / preferenceScreen&GT;
(如的JavaDoc见过 preferenceActivity
)
这将创建在preference UI的条目,点击后会启动一个活动的指定意图
。
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.
- Is there a non deprecated equivalent?
- If not, what if I use it anyway?
- 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
.
这篇关于非德precated发现preference()方法? - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!