android - 如何访问具有膨胀布局的自定义 DialogPreference 中的小部件?

标签 android android-preferences

我是 android 的新手,我正在尝试从我的自定义 DialogPreference 加载/保留值。目前,这失败了,因为 findViewById 返回 null。我(尝试)这样做的方式是否正确?如何在代码中访问我的 EditText 小部件?

public class AddressDialogPreference extends DialogPreference {

public AddressDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    setDialogLayoutResource(R.layout.address_dialog);
}

@Override
protected void onBindDialogView(View view) {

    EditText idField = (EditText) view.findViewById(R.id.hostID);
    EditText ipField = (EditText) view.findViewById(R.id.hostIP);

    SharedPreferences pref = getSharedPreferences();
    idField.setText(pref.getString(getKey() + "_id","ExampleHostname"));
    ipField.setText(pref.getString(getKey() + "_ip","192.168.1.1"));

    super.onBindDialogView(view);
}

@Override
protected void onDialogClosed(boolean positiveResult) {

    if(!positiveResult)
        return;

    Dialog myDial = getDialog();
    EditText idField = (EditText) myDial.findViewById(R.id.hostID);
    EditText ipField = (EditText) myDial.findViewById(R.id.hostIP);

    SharedPreferences.Editor editor = getEditor();
    editor.putString(getKey() + "_id",idField.getText().toString());
    editor.putString(getKey() + "_ip",ipField.getText().toString());
}

地址_dialog.xml:

<TextView
    android:text="Insert IP address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/hostIP" />

<TextView
    android:text="Insert identifier"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/hostID" />

最佳答案

好吧,我自己发现了。好吧,我仍然不知道是什么导致了错误,但我对布局和代码做了很多更改,突然间它就可以工作了。我试图恢复到我在此处发布的代码,但我无法重现该错误。我发布了我的工作代码,所以任何遇到这个问题的人都可以使用它。

管理员也可以选择删除此帖子,因为可能无法重现错误。

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:text="Insert IP address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/AddressBox" />

<TextView
    android:text="Insert identifier"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/HostnameBox" />
</LinearLayout>

和 AddressDialogPreference.java:

public class AddressDialogPreference extends DialogPreference {

private EditText ipBox;
private EditText hostBox;

public AddressDialogPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    setDialogLayoutResource(R.layout.address_dialog);
}

@Override
protected void onBindDialogView(View view) {

    ipBox = (EditText) view.findViewById(R.id.AddressBox);
    hostBox = (EditText) view.findViewById(R.id.HostnameBox);

    SharedPreferences pref = getSharedPreferences();

    hostBox.setText(pref.getString(getKey() + "_host","ExampleHostname"));
    ipBox.setText(pref.getString(getKey() + "_ip","192.168.1.1"));

    super.onBindDialogView(view);
}

@Override
protected void onDialogClosed(boolean positiveResult) {

    if(!positiveResult)
        return;

    SharedPreferences.Editor editor = getEditor();
    editor.putString(getKey() + "_host",hostBox.getText().toString());
    editor.putString(getKey() + "_ip",ipBox.getText().toString());
    editor.commit();

    super.onDialogClosed(positiveResult);
}
}

关于android - 如何访问具有膨胀布局的自定义 DialogPreference 中的小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5722531/

相关文章:

java - React Native : java. util.zip.ZipException : duplicate entry: com/google/android/gms/iid/zzc. 类

android - 动态更改android首选项的widgetlayout资源

java - 无法使 PreferenceFragment 工作(编译错误)

android - 如何动画和控制 Android PreferenceFragment 屏幕?

android - 使用 Universal Image Loader 在 vi​​ewpager 上启用缩放?

Android - 无法注册标题布局

android - 使用 SeekBar 更改 TextView 大小

Android循环显示动画太快

Android:首选项类中的共享首选项编辑器

静态方法中的 Android PreferenceManager