Android findViewById() in Benutzerdefinierten Ansicht
Ich geschrieben habe meine eigene Ansicht, und ich möchte zu aktualisieren, einige andere Ansichten, nach der Interaktion mit meine benutzerdefinierten Ansicht.
Main Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/display_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_centerHorizontal="true"
android:tag="name"
android:ems="10" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/id_number"
android:layout_centerHorizontal="true"
android:layout_below="@id/display_name"/>
<ge.altasoft.custom_views.IdNumber
android:id="@+id/custom_id_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/id_number"
android:paddingLeft="35dip"
custom:firstName="@id/display_name"/>
</RelativeLayout>
Custom Layout Anzeigen:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/id_number_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:paddingRight="35dip" />
<ImageButton
android:id="@+id/load_data_button"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_centerVertical="true"
android:src="@drawable/load_data"
android:layout_toRightOf="@id/id_number_custom" />
</RelativeLayout>
Benutzerdefinierte View-Klasse, Konstruktor und Zuhörer:
private int firstNameViewID;
public IdNumber (Context context, AttributeSet attrs) {
super(context, attrs);
initViews();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IdNumber);
final int N = a.getIndexCount();
for(int i = 0; i < N; i++){
int attr = a.getIndex(i);
switch(attr){
case R.styleable.IdNumber_firstName:
firstNameViewID = a.getResourceId(attr, -1);
break;
}
}
a.recycle();
}
private void initViews() {
inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.id_number_edit_text_custom, this, true);
editText = (EditText) findViewById(R.id.id_number_custom);
loadButton = (ImageButton) findViewById(R.id.load_data_button);
loadButton.setVisibility(RelativeLayout.INVISIBLE);
loadData();
}
private void loadData(){
loadButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText firstName = (EditText) findViewById(R.id.display_name);
firstName.setText("Some Text");
}
});
}
Das Problem ist, dass EditText firstName = (EditText) findViewById(R.id.display_name);
null zurück.
Ich weiß, dass nur zum telefonieren findViewById()
suchen wird die Ansicht in die layout, die ich aufgeblasen.
Wenn es möglich ist, wie bekomme ich die Ansicht von EditText mit der id: display_name von der Haupt-layout ?
Vielen Dank im Voraus.
InformationsquelleAutor der Frage Jilberta | 2013-08-29
Du musst angemeldet sein, um einen Kommentar abzugeben.
versuchen wie diese.
InformationsquelleAutor der Antwort Siddhesh
Ändern Sie Ihre Methode wie folgt, und überprüfen Sie es funktioniert
InformationsquelleAutor der Antwort Usman Kurd
Wenn es mit festem layout können Sie so machen:
Wenn Sie möchten, finden Sie den EditText in ein flexibles layout, werde ich Ihnen später helfen. Hoffe, dass dies helfen.
InformationsquelleAutor der Antwort hieuxit
InformationsquelleAutor der Antwort Dhaval Patel
Versuchen, diese in Ihrem Konstruktor
InformationsquelleAutor der Antwort VijayRaj
Können Sie versuchen, so etwas wie dieses:
Innen customview Konstruktor:
Weiter innen customview können Sie anrufen:
Innen
MainAcivity
definieren:Es funktioniert für mich.
InformationsquelleAutor der Antwort Slavomir Fabian