簡體   English   中英

如何將文本從EditText字段添加到android studio中的不同TextViews

[英]How do I add text from an EditText field to different TextViews in android studio

我對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

實現此目的的簡單方法是每當單擊每個按鈕時就動態創建一個新的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

然后,當您單擊按鈕時,它將動態添加到每個布局。

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

在每個按鈕上單擊,您可以創建新的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);

我針對您的案例的解決方案非常簡單,基本上,我將檢測到按下了哪個按鈕並將其傳遞給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.
            }
    }
}

如果您希望每個小組有兩個以上的名稱,請為每個額外的textView添加“ else if”。 如果您不希望在嘗試添加第三個名稱之前在屏幕上顯示兩個以上的TextView,則可以制作第三個(或更多個)TextView,將其與所需的所有內容對齊,然后將其放置它對GONE的可見性。 這樣,在第3次按下該團隊的按鈕之前,它不會占據任何空間。 在Java中,您需要添加一行代碼

tv3.setVisibility(View.VISIBLE);

我希望這有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM