如何在自定义的DialogPreference中访问具有充气布局的小部件?

15

我很新手,正在尝试从我的自定义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());
}

address_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中,ID(如hostID)内部不应使用大写字母。 - Iulius Curt
1个回答

22

好的,我自己找到了答案。虽然我仍然不知道是什么导致了错误,但我对布局和代码进行了许多更改,突然之间它就起作用了。 我尝试恢复到我在这里发布的代码,但我无法重现错误。 我会发布我的可工作的代码,所以任何遇到这个问题的人都可以使用它。

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

以下是布局:

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

警告:此问答涉及旧的偏好设置。新的 API(从 v7 支持库)使用略有不同的方法。请参见 https://dev59.com/nmQo5IYBdhLWcg3wXeb5#33842737。 - Grzegorz Dev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接