0

我有一个偏好片段,在其中我需要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();

    }
}
4

1 回答 1

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;
    }
});

}

}
于 2013-06-24T14:07:54.500 回答