2017-02-13 39 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); 

行我希望這有助於

相關問題