2016-04-09 16 views
0

(tt) zeigt keinen durch setText() festgelegten Text an. Ich kann die Ausgabe in TextView nicht sehen. Ich habe versucht, mit Toast und setText(), aber in beiden Fällen bekomme ich leere Ausgabe. Warum bekomme ich diese leere Ausgabe?TextView ist leer, wenn setText() gesetzt wurde

package com.divik.ds; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener { 
EditText ed1,ed2,ed3; 
String x,y,z,xx; 
TextView tt; 
Button btn; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ed1=(EditText) findViewById(R.id.editText1); 
    ed2=(EditText) findViewById(R.id.editText2); 
    ed3=(EditText) findViewById(R.id.editText3); 
    tt= (TextView) findViewById(R.id.textView4); 
    btn=(Button) findViewById(R.id.button1); 
    try{ 
    x=(ed1.getText().toString()); 
    y=(ed2.getText().toString()); 
    z=(ed3.getText().toString()); 
    xx=x+""+y+""+z; 
    btn.setOnClickListener(this); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 

} 
@Override 
public void onClick(View v) { 

     try{ 
    tt.setText(xx); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 

Toast.makeText(this,xx,Toast.LENGTH_LONG).show(); 
}} 

Hier ist Xml-Code. Ich habe 3 EditText s verwendet, 4 TextView s und 1 Button

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter Date" 
    android:textSize="30dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" > 


</EditText> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter month" 
    android:textSize="30dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/editText2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" > 


</EditText> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter year" 
    android:textSize="30dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<EditText 
    android:id="@+id/editText3" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" > 


    </EditText> 

<TextView 
    android:id="@+id/textView4" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 

    android:textSize="30dp" /> 


<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 
    </LinearLayout> 

Antwort

0

Verwenden Sie diesen Code statt: -

package com.divik.ds; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener { 
EditText ed1,ed2,ed3; 
String x,y,z,xx; 
TextView tt; 
Button btn; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ed1=(EditText) findViewById(R.id.editText1); 
    ed2=(EditText) findViewById(R.id.editText2); 
    ed3=(EditText) findViewById(R.id.editText3); 
    tt= (TextView) findViewById(R.id.textView4); 
    btn=(Button) findViewById(R.id.button1); 
    btn.setOnClickListener(this); 

} 
@Override 
public void onClick(View v) { 

try{ 
    x=(ed1.getText().toString()); 
    y=(ed2.getText().toString()); 
    z=(ed3.getText().toString()); 
    xx=x+""+y+""+z; 
    tt.setText(xx); 

    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 



Toast.makeText(this,xx,Toast.LENGTH_LONG).show(); 
}} 
+0

Lassen Sie mich wissen, wenn dieser Code nicht funktioniert. Und wenn es funktioniert, dann markieren Sie diese Antwort als akzeptiert. –

+0

ja es funktioniert .thnx – RisingSun

+0

@RisingSun Stellen Sie sicher, dass Sie verstehen, *** warum *** es funktioniert, denn das wird Ihnen in Zukunft mehr nützen, als dass es * jetzt * funktioniert. – codeMagic

3

Seit

x=(ed1.getText().toString()); 
y=(ed2.getText().toString()); 
z=(ed3.getText().toString()); 
xx=x+""+y+""+z; 

in onCreate() sind, werden sie leer sein, wenn die Activity startet zuerst.

Sie können es reparieren, indem Sie diese Zeilen in Ihre onClick() setzen.

@Override 
public void onClick(View v) { 
    try{ 
     x= ed1.getText().toString(); 
     y= ed2.getText().toString(); 
     z= ed3.getText().toString(); 
     xx=x+""+y+""+z; 
     tt.setText(xx); 
    } catch(Exception e) { 
      e.printStackTrace(); 
    } 

    Toast.makeText(this,xx,Toast.LENGTH_LONG).show(); 
}} 
Verwandte Themen