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

栏目: Android · 发布时间: 5年前

内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/5722531/how-to-access-widgets-in-a-custom-dialogpreference-with-a-inflated-layout
我非常新的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());
}

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

代码日志版权声明:

翻译自:http://stackoverflow.com/questions/5722531/how-to-access-widgets-in-a-custom-dialogpreference-with-a-inflated-layout


以上所述就是小编给大家介绍的《android – 如何使用膨胀的布局在自定义DialogPreference中访问窗口小部件?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java多线程编程实战指南(设计模式篇)

Java多线程编程实战指南(设计模式篇)

黄文海 / 电子工业出版社 / 2015-10 / 59.00

随着CPU 多核时代的到来,多线程编程在充分利用计算资源、提高软件服务质量方面扮演了越来越重要的角色。而 解决多线程编程中频繁出现的普遍问题可以借鉴设计模式所提供的现成解决方案。然而,多线程编程相关的设计模式书籍多采用C++作为描述语言,且书中所举的例子多与应用开发人员的实际工作相去甚远。《Java多线程编程实战指南(设计模式篇)》采用Java(JDK1.6)语言和UML 为描述语言,并结合作者多......一起来看看 《Java多线程编程实战指南(设计模式篇)》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具