问题
I'm relatively new to java and new to android studio, so any explanation would be greatly appreciated.
I want to create an app that allows the user to enter someones name and assign them to one of two teams. I am at the point where the user can add one name to each team but I am unsure how to add multiple names to each team.
In my XML I have a EditText field to enter the name, two buttons to put them in Team 1 or Team 2 and two TextViews to display all the people in each team.
<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"/>
Here is my java code, I have set each button to add the name entered to the TextView for team 1 or team 2, depending on what button was selected.
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
回答1:
You can add the names to the same textview by using append(). This will reduce the number of textview objects required as suggested in the first answer.
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:
Easy way to implement this is to dynamically create a new
Textview
whenever each of the buttons is clicked
First of all, you might need to create two LinearLayout
for each team.
And onClick
method should be changed like this code.
TextView textView = new TextView(this); // create a new textview
textView.setText(inputText.getText().toString());
myLinearLayout.addView(textView); // add the textview to the linearlayout
Then it will be added to each layout dynamically when you click the button.
回答3:
On each Button Click You can create new 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:
My solution to your case is quite simple, basically, I would detect which button is being pressed and pass it to the Java. To begin with, in each button of the XML I tend to add the property "onClick" and assign it a function. This is basically the same as setOnClickListener in the Java, but much faster and easier. Secondly, as your buttons have Id, make use of them to recognize which button is being pressed, and according to that, run a code. Here is the code:
<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"/>
The benefit of using Id's is that you can use the same function for each button. The java, based on what I explained before, can look like this.
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.
}
}
}
In case you want to have more than 2 names per team, add "else if"s for each extra textView. If you don't want to show more than 2 TextViews in the screen until a 3rd name is attempted to be added, th``en you can make a 3rd (or more) TextView, align it and everything you need, but then put its visibility to GONE. That way, it won't occupy any space until the button for that team is pressed a 3rd time. In the java, you would need to add a line of code
tv3.setVisibility(View.VISIBLE);
I hope this helps
来源:https://stackoverflow.com/questions/42207413/how-do-i-add-text-from-an-edittext-field-to-different-textviews-in-android-studi