2011-02-14 2 views
0
public EditText text; 
public TextView text1; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

} 
     public void act(View v) { 
      text = (EditText) findViewById(R.id.widget30); 
      text1 = (TextView) findViewById(R.id.textView1); 
      text1.setText(text.getText()); 


     } 

ce code XMLaction à un ImageButton

 <EditText 
android:id="@+id/widget30" 
android:layout_width="260px" 
android:layout_height="50px" 
android:text="Gouvernorat" 
android:textSize="18sp" 
android:layout_x="31px" 
android:layout_y="90px" 
></EditText><ImageButton 
android:layout_width="wrap_content" 
android:src="@drawable/icon1" 
android:id="@+id/imageButton1" 
android:layout_height="wrap_content" 
android:layout_x="108dip" 
android:layout_y="360dip"> 
android:onClick="act" 
</ImageButton><TextView 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:id="@+id/textView1" 
android:text="TextView" 
android:layout_x="196dip" 
android:layout_y="382dip"> 
</TextView> 

comment puis-je dans le champ textview le contenu de EditText?

Merci

Répondre

1

Je ne sais pas si cela est directement copié à partir du code source, mais il y a une faute de frappe dans votre xml. L'attribut onClick est en dehors de votre balise ImageButton. A part ça, vous devez appeler toString sur ce que vous obtenez de EditText.getText. La méthode getText retourne un objet de type éditable et non la chaîne sous-jacente

Essayez ceci pour votre méthode act

public void act(View v) { 
     text = (EditText) findViewById(R.id.widget30); 
     text1 = (TextView) findViewById(R.id.textView1); 
     text1.setText(text.getText().toString()); 
2

Votre code ressemble incompelete:

Je vous écris un bloc de code exemple pour comprendre toute la logique:

private ImageButton button; 
    private EditText et; 
    private TextView tv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); // your layout file name 

     button = (ImageButton) findViewById(R.id.id_of_image_button); // your image button 
     et = (EditText) findViewById(R.id.id_of_edit_text); // your edit text field 
     tv = (TextView) findViewById(R.id.id_of_text_view); // your text view 

     // click event on your button 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // Do something with the value of the button 
       // sets the value of the edit text field to the text view 
       tv.setText(et.getText().toSting()); 
      } 
     }); 
    } 

De même, vous devez écrire votre fichier de mise en page XML de manière correcte.