如何将文本从EditText字段添加到android studio中的不同TextViews [英] How do I add text from an EditText field to different TextViews in android studio

查看:143
本文介绍了如何将文本从EditText字段添加到android studio中的不同TextViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java和android studio还是比较陌生,所以任何解释都将不胜感激.

I'm relatively new to java and new to android studio, so any explanation would be greatly appreciated.

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

I want to create an app that allows the user to enter someones name and assign them to one of two teams. I am at the point where the user can add one name to each team but I am unsure how to add multiple names to each team.

在我的XML中,我有一个EditText字段来输入名称,两个按钮将它们放在Team 1或Team 2中,两个TextViews显示每个团队中的所有人.

In my XML I have a EditText field to enter the name, two buttons to put them in Team 1 or Team 2 and two TextViews to display all the people in each team.

    <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中的名称添加到该名称中,具体取决于选择了哪个按钮.

Here is my java code, I have set each button to add the name entered to the TextView for team 1 or team 2, depending on what button was selected.

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

推荐答案

您可以使用append()将名称添加到同一textview中.这将减少第一个答案中建议的所需textview对象的数量.

You can add the names to the same textview by using append(). This will reduce the number of textview objects required as suggested in the first answer.

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() + "\n"; // add new line so each person is on their own line
            TextView newText = (TextView) findViewById(R.id.team1_person1);
            newText.append(str); // change setText to append.
        }
    });

这篇关于如何将文本从EditText字段添加到android studio中的不同TextViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆