2016-05-12 22 views
0

Ich habe eine Frage, ich möchte eine zufällige Sentense erstellen. Bei meiner ersten Aktivität habe ich 4 EditText (für den Namen des Spielers).Verwenden Sie EditText für TextView

Ich habe den Code zum Generieren einer zufälligen Sentense, aber ich wusste nicht, wie den Wert von EditText hinzufügen.

MainActivity: 4 EditText RandomActivity: Zeigen Sie einige Sätze aus meinem String.

ich machen möchte ein sentense wie: [EDITTEXT1] WORD WORD WORD [EDITEXT2]

Können Sie versuchen, mir zu erklären, wie zu tun? Vielen Dank !

RandomActivity:

Resources res = getResources(); 

     myString = res.getStringArray(R.array.phrases); 

     String q = myString[rgenerator.nextInt(myString.length)]; 

     tv = (TextView) findViewById(R.id.textView); 
     tv.setText(q); 

Meine XML-Datei für sentenses

<string-array name="phrases"> // name of the string array 
    <item>Phrase 1</item> // items 
    <item>Phrase 2</item> 
    <item>Phrase 3</item> 
    <item>Phrase 4</item> 
</string-array> 

Meine XML für EditText

<EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:text="Joueur 1" 
     android:textColor="#FFF" 

     android:layout_centerHorizontal="true" 
     android:layout_marginTop="114dp" 
     android:autoText="true" 
     android:editable="true" 
     android:maxWidth="200dp" 
     android:minWidth="200dp" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText2" 
     android:text="Joueur 2" 
     android:textColor="#FFF" 
     android:layout_below="@+id/editText" 
     android:layout_alignLeft="@+id/editText" 
     android:layout_alignStart="@+id/editText" 
     android:minWidth="200dp" 
     android:maxWidth="200dp" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText3" 
     android:text="Joueur 3" 
     android:textColor="#FFF" 
     android:minWidth="200dp" 
     android:maxWidth="200dp" 
     android:layout_below="@+id/editText2" 
     android:layout_alignLeft="@+id/editText2" 
     android:layout_alignStart="@+id/editText2" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText4" 
     android:text="Joueur 4" 
     android:textColor="#FFF" 
     android:minWidth="200dp" 
     android:maxWidth="200dp" 
     android:layout_below="@+id/editText3" 
     android:layout_alignLeft="@+id/editText3" 
     android:layout_alignStart="@+id/editText3" /> 

Antwort

0
private String generateString() { 
    Context context; 
    String [] phrases = getResources().getStringArray(R.array.phrases); 
    StringBuilder stringBuilder = new StringBuilder(); 

    return stringBuilder 
      .append(editeText.getText().toString()) 
      .append(phrases[0]) 
      .append(phrases[1]) 
      .append(editeText2.getText().toString()) 
      .toString(); 
} 

Wenn Textview hat vale "Apple- ", textView 2 hat einen Wert" Orange " Mit dieser Methode erhalten Sie eine Zeichenfolge, die" Hello phrase1 phrase2 Orange "lautet.

So verwenden String Builder können Sie append() wie ich oben getan haben Sie können fügen Sie Ihre Zeichenfolge anhängen.

Verwandte Themen