मैं एंड्रॉइड के लिए बहुत नया हूं और मैं अपने अनुकूलित डायलॉग प्रेफरेंस से मूल्यों को लोड/जारी रखने की कोशिश कर रहा हूं। वर्तमान में, यह विफल रहता है क्योंकि findViewById शून्य लौटाता है। क्या मैं इसे सही करने के लिए कोशिश करता हूं? कोड में मेरे संपादन टेक्स्ट विजेट तक पहुंच कैसे प्राप्त करूं?एक बढ़ते लेआउट के साथ एक कस्टम DialogPreference में विजेट का उपयोग कैसे करें?
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" />
आप ('hostID' की तरह) एक्सएमएल में आईडी के अंदर बड़े अक्षरों का उपयोग नहीं करना चाहिए
यहाँ लेआउट है –