我是自学成才,坠入爱河。从理论上讲,这是我的第一个android应用程序,非常简单。
问题:我已经成功地使用layoutinflator方法单击按钮来动态创建新的EditText。我的问题是,能够识别那些EditText的新ID,或者只是从用户创建的新EditText中检索新的输入值...,这样我就可以将其显示到各自的TextViews中。
谢谢你。我衷心感谢。
MainActivity.java代码:
public class MainActivity extends AppCompatActivity {
LinearLayout parentLinearLayout;
Button button;
TextView result;
EditText editName;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parentLinearLayout = findViewById(R.id.parent_linear_layout);
editName = findViewById(R.id.editName);
result = findViewById(R.id.textViewName);
button = findViewById(R.id.addName);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editName.getText().toString();
result.setText(name);
}
});
}
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.activity_row, null);
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
public void onDelete(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical" >
<Button
android:id="@+id/add_field_button"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="#555"
android:onClick="onAddField"
android:text="Add Player"
android:textColor="#FFF"
/>
<TextView
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name 1"
/>
<TextView
android:paddingTop="5dp"
android:paddingLeft="5dp"
android:id="@+id/textViewName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name 2"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/editName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5" />
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete" />
</LinearLayout>
<Button
android:id="@+id/addName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
activity_row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="@+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5" />
<Button
android:id="@+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="@android:drawable/ic_delete"
android:onClick="onDelete"/>
</LinearLayout>
尝试从膨胀视图中获取EditText
和TextView
,并在TextChangedListener
上添加EditText
并更新TextView
内的onTextChanged
public void onAddField(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.activity_row, null);
EditText editText = rowView.findViewById(R.id.number_edit_text);
TextView textView = rowView.findViewById(R.id.your_text_view);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
textView.setText(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}