点击时不会调用Android OnPreferenceClick

时间:2013-06-24 14:00:40

标签: android android-preferences

我有一个偏好片段,我在setOnClickListener()调用首选项并覆盖onPreferenceClick()方法。但是,单击首选项时不会调用我的新方法。

我可以通过将代码移动到我的自定义ClickListener类来使EditTextPreference工作,但必须启用首选项才能触发侦听器。

我想使用此首选项作为标签来显示值,并在点击它时显示更改它的选项。我不希望用户直接编辑它。

我怎样才能做到这一点。?

代码如下

public class PrefActivity extends PreferenceActivity {

    static final String TAG="Preference Activity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    }


    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }


    public static class MyPreferenceFragment extends PreferenceFragment 
    {
        @Override
        public void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            Preference clickedPreference = (Preference) findPreference("userLocation");
            clickedPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {

                @Override
                public boolean onPreferenceClick(Preference preference) {
                    // TODO Auto-generated method stub
                    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(preference.getContext());
                    boolean locationTypeSetting;
                    LocationSetting locationSetting = new LocationSetting(preference.getContext());
                    String clickedPreference = preference.getKey();

                    if (clickedPreference.equals("userLocation")){
                        locationTypeSetting = settings.getBoolean("locationSetting", false);
                        if (locationTypeSetting == true)
                        {
                            AlertDialog.Builder noEditDialogBuilder = new AlertDialog.Builder(preference.getContext());
                            noEditDialogBuilder.setTitle(R.string.no_edit_dialog_title);
                            noEditDialogBuilder
                            .setMessage(R.string.no_edit_dialog_message)
                            .setPositiveButton(R.string.no_edit_dialog_positive_action,null);
                            AlertDialog noEditDialog = noEditDialogBuilder.create();
                            noEditDialog.show();
                        }
                        else
                            locationSetting.displayMap(0);
                    }

                    return false;
                            }

                        });
                    }

                }
    }

我的偏好XML

<SwitchPreference
    android:defaultValue="0"
    android:key="locationSetting"
    android:summary="Automatic Location"
    android:switchTextOff="OFF"
    android:switchTextOn="ON" />

<com.example.test.EditTextPreferenceWithSummary
    android:key="userLocation"
    android:title="Address"
    android:shouldDisableView="false"
    android:enabled="false"
    android:selectable="true" />

我的EditTextPreferenceWithSummary类

public class EditTextPreferenceWithSummary extends EditTextPreference {
    private final static String TAG = EditTextPreferenceWithSummary.class.getName();

    @Override
    protected View onCreateView(ViewGroup parent) {
        this.setSummary(this.getText());
        return super.onCreateView(parent);
    }

    public EditTextPreferenceWithSummary(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EditTextPreferenceWithSummary(Context context) {
        super(context);
        init();
    }

    private void init() {
        Log.e(TAG, "init");
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.getContext());     
        String currentText =  prefs.getString("userLocation", this.getText());


        this.setSummary(currentText);

        setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                preference.setSummary(newValue.toString()); 
                return true;
            }
        });
    }
}

我删除了片段并直接使用了类。仍然是同样的问题

代码如下

public class PrefActivity extends PreferenceActivity {

    static final String TAG="Preference Activity";

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        Preference clickedPreference = (Preference) findPreference("userLocation");
        clickedPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                // TODO Auto-generated method stub
                SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(preference.getContext());
                boolean locationTypeSetting;
                LocationSetting locationSetting = new LocationSetting(preference.getContext());
                String clickedPreference = preference.getKey();

                if (clickedPreference == "userLocation"){
                    locationTypeSetting = settings.getBoolean("locationSetting", false);
                    if (locationTypeSetting == true)
                    {
                        AlertDialog.Builder noEditDialogBuilder = new AlertDialog.Builder(preference.getContext());
                        noEditDialogBuilder.setTitle(R.string.no_edit_dialog_title);
                        noEditDialogBuilder
                        .setMessage(R.string.no_edit_dialog_message)
                        .setPositiveButton(R.string.no_edit_dialog_positive_action,null);
                        AlertDialog noEditDialog = noEditDialogBuilder.create();
                        noEditDialog.show();
                    }
                    else
                        locationSetting.displayMap(0);
                }

                return false;
                        }

                    });
                }



    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

    }
}

1 个答案:

答案 0 :(得分:0)

试试这个会帮助你

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class Reset extends PreferenceActivity{

private Preference myPreference;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
myPreference = findPreference("reset");
myPreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    public boolean onPreferenceClick(Preference arg0) {
        Builder alertDialog = new AlertDialog.Builder(Reset.this);
        alertDialog.setMessage("Reset?");
        alertDialog.setCancelable(true);
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                SharedPreferences settings =   PreferenceManager.getDefaultSharedPreferences(getBaseContext());                          
                SharedPreferences.Editor editor = settings.edit();
                editor.clear();
                editor.commit();
            } }); 
        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            } }); 
        alertDialog.show();
        return false;
    }
});

}

}