本文介绍了如何申请在preferenceClick条件的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的我的prefnces.xml

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


  <ListPreference
     android:entries="@array/listOptions"
     android:entryValues="@array/listValues"
     android:key="listpref"
     android:summary="List preference example"
     android:title="List preference" />
 </PreferenceCategory>
</PreferenceScreen>

我进入3值 array.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
   <string-array name="listOptions">
     <item>Facebook</item>
     <item>Gmail</item>
     <item>Twitter</item>
     </string-array>

   <string-array name="listValues">
     <item>1 </item>
     <item>2 </item>
     <item>3</item>
   </string-array>
 </resources>

preferencesActivityTest.class

public class PreferencesActivityTest extends PreferenceActivity {
    public static final String PREF_BEER_SIZE = "listpref";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.my_prefrences);
        Preference beerPref = (Preference) findPreference(PREF_BEER_SIZE);
        beerPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                // how will apply function for each facebook twitter and gmail
                return false;

            }
        });
    }
}

我想分享我的应用程序链接我已经做了在另一个应用程序我有code此共享,但我在与适用条件问题,我们如何将应用功能,我们选择Gmail或Facebook或Twitter意味着对项目选择此prefnces我做的第一次。

I want share my application link I have done sharing in another application I have code for this but I'm having problem with apply the condition how we'll apply function we select gmail or facebook or twitter mean on item select this prefnces I'm doing first time.

推荐答案

您应该使用在preferenceChangelistener而不是preferenceClickListener。

You should use OnPreferenceChangelistener rather than onPreferenceClickListener.

在preferenceClickListener是单击preference之前做一些工作。在preferenceclick你将有什么比较,因为它是在视图中点击。没有被设置的值

OnPreferenceClickListener is to do some task before click on the preference. In Preferenceclick you will have nothing to compare since it is a click on the view. Not the value being set

 beerPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
     public boolean onPreferenceChange(Preference pref, Object newValue) {
         final ListPreference listPref = (ListPreference) pref;
         final int idx = listPref.findIndexOfValue((String) newValue);

         //Depending upon idx execute
         return true;
     }
 });

这篇关于如何申请在preferenceClick条件的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 20:35