如何使用膨胀布局访问自定义 DialogPreference 中的小部件?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我自己发现了。好吧,我仍然不知道是什么导致了错误,但我对布局和代码做了很多更改,突然间它就工作了。我尝试恢复到我在此处发布的代码,但无法重现该错误。我发布了我的工作代码,因此遇到此问题的任何人都可以使用它。
管理员也可以选择删除此帖子,因为可能无法重现该错误。
这是布局:
和 AddressDialogPreference.java:
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:
and the AddressDialogPreference.java: