我有我自己的佈局。安卓編輯文本偏好與自己的佈局,onPreferenceChange上沒有newValue
我希望在單擊佈局上的正面按鈕後從editText接收文本。但在onPreferenceChange中,我總是隻獲取默認值。
看來,我需要以某種方式將自己的EditText綁定到首選項,但我不知道如何以及在何處執行此操作。
任何人都可以幫助我嗎?
我有我自己的佈局。安卓編輯文本偏好與自己的佈局,onPreferenceChange上沒有newValue
我希望在單擊佈局上的正面按鈕後從editText接收文本。但在onPreferenceChange中,我總是隻獲取默認值。
看來,我需要以某種方式將自己的EditText綁定到首選項,但我不知道如何以及在何處執行此操作。
任何人都可以幫助我嗎?
要回答我的問題:
首先,在PreferenceScreen,你需要的狀態: < full.path.to.your.OwnLayoutClass 機器人:名字= 「whatevever」 機器人:dialogLayout = 「@ layout/your_own_layout」/ >
your_own_layout可以是任何你想要的,按鈕,editTexts,根據你的願望linearlayout。
基本是表示您自己的偏好對話框的類。這裏是如何做到這一點簡單的例子:
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