【问题标题】:How do I add text from an EditText field to different TextViews in android studio如何将 EditText 字段中的文本添加到 android studio 中的不同 TextView
【发布时间】:2017-02-13 15:16:30
【问题描述】:

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

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

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

    <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

【问题讨论】:

    标签: java android xml button android-edittext


    【解决方案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
    

    然后它会在你点击按钮时动态添加到每个布局中。

    【讨论】:

      【解决方案2】:

      您可以使用 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.
              }
          });
      

      【讨论】:

        【解决方案3】:

        在每个按钮点击你可以创建新的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);
        

        【讨论】:

          【解决方案4】:

          我对您的情况的解决方案非常简单,基本上,我会检测正在按下哪个按钮并将其传递给 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,直到尝试添加第三个名称,那么您可以制作第三个(或更多)TextView,将其与您需要的所有内容对齐,然后放它对 GONE 的可见性。这样,在第三次按下该团队的按钮之前,它不会占用任何空间。在java中,你需要添加一行代码

          tv3.setVisibility(View.VISIBLE);
          

          希望对你有帮助

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-08
            • 2011-06-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-01-27
            相关资源
            最近更新 更多