Android编辑文本首选项具有自己的布局,onPreferenceChange没有newValue

时间:2012-08-27 11:12:41

标签: android android-edittext android-preferences

我有自己的布局。

我希望在点击布局上的正面按钮后从editText接收文本。但是在onPreferenceChange中,我总是只得到默认值。

似乎我需要以某种方式将自己的EditText绑定到首选项,但我不知道如何以及在何处执行此操作。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:2)

回答我自己的问题:

首先,在PreferenceScreen中,您需要声明:      < full.path.to.your.OwnLayoutClass        机器人:名字= “whatevever”        android:dialogLayout =“@ layout / your_own_layout”/>

your_own_layout可以是您想要的任何内容,可以根据您的意愿使用按钮,editTexts进行linearlayout。

Essential是表示您自己的首选项Dialog的类。以下是如何执行此操作的简单示例:

public class YourOwnLayoutClass extends DialogPreference {

    private LinearLayout mView;

    public YourOwnLayoutClass(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(false);
        setDialogLayoutResource(R.layout.your_own_layout);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        mView = (LinearLayout) view;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult) {
            // get value from editFields, do whatever you want here :)
            // you can acces them through mView variable very easily
        }
    }
}

重要参考资料: I need to have a custom dialog in Preferences

Concise way of writing new DialogPreference classes?

Android: launch a custom Preference from a PreferenceActivity

相关问题