如何使用膨胀布局访问自定义 DialogPreference 中的小部件?

发布于 2024-11-02 22:22:01 字数 1858 浏览 6 评论 0原文

我对 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());
}

地址对话框.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" />

Im very new to android and Im trying to load/persist values from my customized DialogPreference. Currently, this fails because findViewById returns null. Is the way I (try) to do it correct? How do I get access to my EditText widgets in the code?

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" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁拥 2024-11-09 22:22:01

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

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

这是布局:

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

Ok I found it out myself. Well, I still do not know what caused the error, but I did a lot of changes to the layout and code and suddenly it just worked. I tried to revert to the code that I posted here, but I cannot reproduce the error. Im posting my working code, so anybody who runs into this problem, may use it.

An admin may also choose to delete this post, as it may be not possible to reproduce the error.

Here is the layout:

<?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>

and the 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);
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文