Я самоучка и влюбилась. Это мое первое android приложение, которое в теории довольно просто.
Проблема: мне удалось динамически создать новые EditTexts одним нажатием кнопки, используя метод layoutinflator. Моя проблема в том, что я могу определить новые идентификаторы этих EditTexts или просто получить новые входные значения из новых EditTexts, которые создал пользователь ... так что я могу отобразить их в соответствующих текстовых представлениях.
Заранее спасибо. Я искренне ценю это.
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>