2017-10-28 4 views
-5

Ich habe ein Problem. Ich möchte den Text aus einem EditText-Feld von einer anderen Aktivität als der Schaltfläche ändern.Change EditText mit Button klicken

Hier ist meine Haupt xml:

<EditText 
    android:id="@+id/articlename" 
    android:layout_width="278dp" 
    android:layout_height="75dp" 
    android:layout_marginStart="16dp" 
    android:layout_marginTop="4dp" 
    android:clickable="false" 
    android:cursorVisible="false" 
    android:ems="10" 
    android:focusable="false" 
    android:inputType="textMultiLine" 
    android:text="Name of article" 
    android:textColor="#FFF" 
    android:textSize="22dp" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 
<Button 
    android:id="@+id/button" 
    android:layout_width="90dp" 
    android:layout_height="90dp" 
    android:layout_marginStart="16dp" 
    android:layout_marginTop="108dp" 
    android:background="@drawable/circle" 
    android:onClick="onClicknews" 
    android:text="news\n(click me)" 
    android:textColor="#FFFF" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

Hier mein MainActivity wo der Knopf ist:

public void onClicknews(View v) { 
    Intent intent = new Intent(getApplicationContext(), NewsActivity.class); 
    String titel1 = "Trumps brutales Kalkül"; 
    EditText news = (EditText) findViewById(R.id.news); 
    EditText article = (EditText) findViewById(R.id.articlename); 
    String s = article.getText().toString(); 
    if (s.equalsIgnoreCase(titel1)){ 
     news.setText("Hallo"); 
    } 
    startActivity(intent); 
} 

Und hier meine zweite xml wo die EditText ist:

<EditText 
    android:id="@+id/news" 
    android:layout_width="350dp" 
    android:layout_height="407dp" 
    android:layout_marginBottom="23dp" 
    android:layout_marginEnd="11dp" 
    android:layout_marginStart="11dp" 
    android:clickable="false" 
    android:cursorVisible="false" 
    android:ems="10" 
    android:focusable="false" 
    android:inputType="textMultiLine" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" /> 

I Ich weiß, dass es ähnliche Fragen gibt, aber keine der Lösungen hat mir geholfen.

+0

Warum senden Sie den Wert als Bündel in der Absicht für die zweite Aktivität und extrahieren Sie das in der onCreate und setzen Sie dort? Geben Sie weitere Informationen zu Ihrem Anwendungsfall an ... – kRiZ

Antwort

0

In Ihrem MainActivity.java:

public void onClicknews (Blick v) {

String titel1 = "Trumps brutales Kalkül"; 

EditText article = (EditText) findViewById(R.id.articlename); 
String s = article.getText().toString(); 
Intent intent = new Intent(getApplicationContext(), NewsActivity.class); 
intent.putExtra("articalName",s); 
startActivity(intent); 

}

In Ihrem NewsActivity.java:

EditText news = (EditText) findViewById (R.id.news);

Zeichenfolge newsName = getIntent(). GetStringExtra ("articalName");

news.setText (newsName);

+0

Vielen Dank! –

0

Es gibt keine sichere Möglichkeit, dies nur mit Android Flow zu tun. Android-System kann immer Aktivitäten zerstören, die momentan nicht sichtbar sind, so dass Ihr EditText möglicherweise nicht existiert. Sie können Singleton oder Abhängigkeit mit Dolch erstellen, um Daten zwischen Aktivitäten zu speichern.