写一个Dialogpreference完整代码
时间: 2024-05-07 20:20:04 浏览: 14
DialogPreference是一个基于Dialog的Preference,它允许用户输入和编辑字符串、选择设置等。下面是一个完整的DialogPreference的示例代码:
```java
public class ExampleDialogPreference extends DialogPreference {
private EditText editText;
private String currentValue;
public ExampleDialogPreference(Context context, AttributeSet attrs) {
super(context, attrs);
// 设置Dialog布局
setDialogLayoutResource(R.layout.dialog_example_preference);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
// 获取EditText对象
editText = view.findViewById(R.id.edit_text);
// 设置EditText的初始值
if (currentValue != null) {
editText.setText(currentValue);
}
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
// 获取用户输入的值
String newValue = editText.getText().toString();
// 保存用户输入的值
persistString(newValue);
// 更新Preference的摘要
setSummary(newValue);
}
}
@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
// 从XML中获取默认值
return a.getString(index);
}
@Override
protected void onSetInitialValue(Object defaultValue) {
// 设置Preference的初始值
currentValue = getPersistedString((String) defaultValue);
setSummary(currentValue);
}
}
```
在上面的代码中,我们重写了DialogPreference的几个方法:
- `setDialogLayoutResource(int layoutResId)`:设置Dialog的布局。
- `onBindDialogView(View view)`:在Dialog显示之前获取EditText对象,并设置EditText的初始值。
- `onDialogClosed(boolean positiveResult)`:在Dialog关闭之后获取用户输入的值,并保存到SharedPreferences中,同时更新Preference的摘要。
- `onGetDefaultValue(TypedArray a, int index)`:从XML中获取Preference的默认值。
- `onSetInitialValue(Object defaultValue)`:设置Preference的初始值。
需要注意的是,在我们的DialogPreference中,我们使用了一个自定义的布局文件`dialog_example_preference.xml`,它包含一个EditText和一些其他的UI元素。下面是一个示例布局文件:
```xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/example_preference_title"/>
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>
</LinearLayout>
```
在这个布局文件中,我们使用了一个LinearLayout来包含一个标题TextView和一个EditText。在DialogPreference中,我们可以通过`findViewById()`方法获取EditText对象,并进行相关的操作。
最后,需要在PreferenceScreen中添加我们的自定义Preference:
```xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/example_category_title">
<com.example.app.ExampleDialogPreference
android:key="example_preference"
android:title="@string/example_preference_title"
android:summary="@string/example_preference_summary"
android:dialogTitle="@string/example_preference_dialog_title"
android:defaultValue="@string/example_preference_default_value"/>
</PreferenceCategory>
</PreferenceScreen>
```
在上面的代码中,我们使用了`com.example.app.ExampleDialogPreference`来代替原来的`Preference`,并设置了相关的属性,包括标题、摘要、对话框标题和默认值。这样,我们就完成了一个自定义的DialogPreference。