2017-10-28 9 views
-5

문제가 있습니다. 단추 이외의 다른 활동에서 EditText 필드의 텍스트를 변경하고 싶습니다.버튼으로 편집 문구 변경

다음
<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" /> 

내 MainActivity 버튼은 다음과 같습니다 :

<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 다음 글고 치기가

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); 
} 

그리고 여기 내 두 번째 XML 여기

내 주요 XML이다 비슷한 질문이 있다는 것을 알았지 만 솔로 션 중 어느 것도 나를 도왔습니다.

+0

두 번째 활동의 의도에 값을 번들로 보내고 onCreate에서 해당 값을 추출하여 거기에 설정하는 것이 어떻습니까? 유스 케이스에 대한 자세한 정보를 제공하십시오 ... – kRiZ

답변

0

글고 뉴스 = (글고 치기) findViewById (R.id.news);

문자열 newsName = getIntent(). getStringExtra ("articalName");

news.setText (newsName);

+0

대단히 고마워요! –

0

안드로이드 흐름만을 사용하여 안전하게 할 수있는 방법은 없습니다. Android 시스템은 현재 표시되지 않는 활동을 항상 파괴 할 수 있으므로 EditText가 존재하지 않을 수 있습니다. 단도를 사용하여 단도 또는 종속성을 만들면 활동간에 데이터를 저장할 수 있습니다.

공공 무효 onClicknews (보기 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); 

} 당신의 NewsActivity.java에서

: 당신의 MainActivity.java에서