편집 필드가있는 동적 선형 레이아웃을 추가했습니다. 이제 해당 편집 필드에서 텍스트를 가져오고 싶습니다. 레이아웃을 동적으로 추가하는 코드는 다음과 같습니다.
linearLayoutForm = (LinearLayout) findViewById(R.id.linearLayoutForm);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final LinearLayout newView = (LinearLayout)getLayoutInflater().inflate(R.layout.single_skill_row, null);
newView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
ImageButton btnRemove = (ImageButton) newView.findViewById(R.id.btnRemove);
btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayoutForm.removeView(newView);
}
});
linearLayoutForm.addView(newView,0);
}
});
이 코드로 텍스트를 얻으려고 시도했지만 null 포인터 예외가 발생했습니다.
register_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LinearLayout et2=(LinearLayout) linearLayoutForm.getChildAt(linearLayoutForm.getChildCount());
EditText e = (EditText) et2.getChildAt(et2.getChildCount());
String s=e.getText().toString();
});
여기 single_skill_row의 XML이 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowdetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/editDescricao"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.62"
android:ems="10"
android:inputType="text"
android:maxLines="1">
<!--<requestFocus />-->
</EditText>
<ImageButton
android:id="@+id/btnRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/btn_remove"
android:src="@drawable/ic_remove_black_18dp" />
</LinearLayout>
다음과 같이 할 수 있습니다.
먼저 수업에서 필드를 추가합니다.
...
EditText myEditText;
...
그런 다음 linearLayout을 추가 한 후 다음과 같이 필드에 editText를 저장합니다.
...
final LinearLayout newView = (LinearLayout)getLayoutInflater().inflate(R.layout.single_skill_row, null);
myEditText = newView.findViewById(R.id.editDescricao);
...
그 후 버튼의 onClick 콜백에서 다음과 같은 텍스트를 얻을 수 있습니다.
register_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = myEditText.getText().toString();
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다