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

时间:2017-02-13 15:16:31

标签: java android xml button android-edittext

我是java的新手,也是android studio的新手,所以任何解释都会非常感激。

我想创建一个应用程序,允许用户输入someone名称并将其分配给两个团队中的一个。我正处于用户可以为每个团队添加一个名称的位置,但我不确定如何为每个团队添加多个名称。

在我的XML中,我有一个EditText字段用于输入名称,两个按钮用于输入Team 1或Team 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

4 个答案:

答案 0 :(得分:1)

  

实现此目的的简便方法是动态创建新的Textview   每当点击每个按钮时

首先,您可能需要为每个团队创建两个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

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

答案 1 :(得分:0)

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

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.
        }
    });

答案 2 :(得分:0)

在每个按钮上单击您可以创建新的TextView

TextView textView1 = new TextView(MainActivity.this);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView1.setLayoutParams(tvParams);
textView1.setText("Your Text);
textView1.setBackgroundColor(Color.parseColor("#FF0000"));
textView1.setPadding(0, 5, 0, 10);

答案 3 :(得分:0)

我的解决方案非常简单,基本上,我会检测哪个按钮被按下并将其传递给Java。首先,在XML的每个按钮中,我倾向于添加属性“onClick”并为其分配一个函数。这与Java中的setOnClickListener基本相同,但更快更容易。其次,当您的按钮具有Id时,使用它们来识别正在按下哪个按钮,并根据该按钮运行代码。这是代码:

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

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

<Button
    android:id="@+id/Team2"
    android:onClick="onClick"
    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"/>

使用Id的好处是您可以为每个按钮使用相同的功能。基于我之前解释过的内容,java可能看起来像这样。

public void onClick (View view) {
    String name = NameText.getText().toString;
    int t1Counter = 0;
    int t2Counter = 0;
    switch(view.getId()) {
        case R.id.Team1:
            if (t1Counter == 0) {
                team1_person1.setText(name);
                t1Counter++;
                NameText.setText(""); //Erases the EditText name, as we have saved it already in a team.
            } else if (t1Counter == 1) {
                team1_person2.setText(name);
                t1Counter++;
                NameText.setText("");
            } else {
                Toast.makeText(this, "Team 1 is full", Toast.LENGTH_LONG).show(); //This displays a classic grey alert message for about a second, and doesn't assign the person to a team.
            }
            break;
        case R.id.Team2:
            if (t2Counter == 0) {
                team2_person1.setText(name);
                t2Counter++;
                NameText.setText(""); //Erases the EditText name, as we have saved it already in a team.
            } else if (t2Counter == 1) {
                team2_person2.setText(name);
                t2Counter++;
                NameText.setText("");
            } else {
                Toast.makeText(this, "Team 2 is full", Toast.LENGTH_LONG).show(); //This displays a classic grey alert message for about a second, and doesn't assign the person to a team.
            }
    }
}

如果您希望每个团队拥有2个以上的名字,请为每个额外的textView添加“else if”。如果您不想在屏幕上显示超过2个TextView,直到尝试添加第3个名称,那么您可以制作第3个(或更多)TextView,对齐它和您需要的所有内容,但然后放入它对GONE的可见度。这样,在第三次按下该团队的按钮之前,它不会占用任何空间。在java中,您需要添加一行代码

tv3.setVisibility(View.VISIBLE);

我希望这会有所帮助