java - 如何将 EditText 字段中的文本添加到 android studio 中的不同 TextView

标签 java android xml button android-edittext

我对 java 和 android studio 都比较陌生,所以任何解释将不胜感激。

我想创建一个应用程序,允许用户输入某人的姓名并将其分配给两个团队之一。我现在的情况是,用户可以向每个团队添加一个名称,但我不确定如何向每个团队添加多个名称。

在我的 XML 中,我有一个用于输入姓名的 EditText 字段、两个用于将其放入团队 1 或团队 2 的按钮以及两个用于显示每个团队中所有人员的 TextView。

    <EditText
        android:layout_width="106dp"
        android:layout_height="wrap_content"
        android:id="@+id/NameText"/>

    <Button
        android:id="@+id/Team1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Team 1"/>

    <Button
        android:id="@+id/Team2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Team 2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/team1_person1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/team1_person2"
        android:layout_column="1"/>

这是我的 java 代码,我已设置每个按钮以将输入的名称添加到团队 1 或团队 2 的 TextView 中,具体取决于选择的按钮。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button t1button = (Button) findViewById(R.id.Team1);
        Button t2button = (Button) findViewById(R.id.Team2);


        t1button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // --- find the text view --
                EditText inputText = (EditText) findViewById(R.id.NameText);
                String str = inputText.getText().toString();
                TextView newText = (TextView) findViewById(R.id.team1_person1);
                newText.setText( str);
            }
        });

        t2button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // --- find the text view --
                EditText inputText = (EditText) findViewById(R.id.NameText);
                String str = inputText.getText().toString();
                TextView newText = (TextView) findViewById(R.id.team1_person2);
                newText.setText( str);
            }
        });



    }
}


I know I'll need to add more TextViews for each new name, but I'm not sure how this works with only one button. 
Thanks

最佳答案

Easy way to implement this is to dynamically create a new Textview whenever each of the buttons is clicked

首先,您可能需要为每个团队创建两个 LinearLayout

并且 onClick 方法应该像这样的代码一样更改。

TextView textView = new TextView(this); // create a new textview
textView.setText(inputText.getText().toString());
myLinearLayout.addView(textView); // add the textview to the linearlayout

然后,当您单击按钮时,它将动态添加到每个布局中。

关于java - 如何将 EditText 字段中的文本添加到 android studio 中的不同 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42207413/

相关文章:

java - 如何检查spring security中的多个表以进行身份​​验证

java - Solaris unix NLWP 限制

javascript - Cordova RSS 提要链接不会在外部浏览器中打开

xml - Start_Element 和 End_Element 未被调用

android - 当我在手机(android 6.1 和 api 23)上运行我的应用程序时它崩溃了,但是当我在模拟器(android 7.0 和 api 24)上运行它时它不会崩溃

java - 一个Java链表类定义的解释

java - Android - php mysql。没有更新值

Android Firebase 阵列

Android 在 Fragment 中显示 Snackbar

java - 我需要一个遍历日期间隔的循环