2017-02-13 67 views
0

我对java比较陌生,对于android工作室比较新,所以任何解释都将不胜感激。如何从EditText字段添加文本到Android工作室中的不同TextView

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

在我的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代码,我已经设置每个按钮来添加输入到TextView的球队或1队2,具体取决于所选择哪个按钮的名称。实现这个

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 

回答

0

您可以使用append()将名称添加到同一个textview。这将减少第一个答案中建议的所需的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. 
     } 
    }); 
+0

就是这样排序,万分感谢! –

+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 

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

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); 
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”。如果你不想在屏幕上显示两个以上的TextView,直到试图添加第三个名字,那么你可以制作第三个(或更多)TextView,对齐它和你需要的所有东西,然后把它的可见性GONE。这样,它将不会占用任何空间,直到第3次按下该球队的按钮。在Java中,你将需要添加的代码

tv3.setVisibility(View.VISIBLE); 

行我希望这有助于

相关问题