Je suis très nouveau sur le développement Android et je viens de commencer à étudier. Ce que j'essaye est d'ajouter un bouton et quand ce bouton est pressé un texte "mon premier projet" pour être affiché dans la vue de texte.afficher une chaîne sur la vue texte lorsque vous cliquez sur un bouton dans android
Avec l'aide de quelques experts j'ai créé la vue de bouton et de texte. Donc, le bouton apparaît dans le simulateur mais quand je clique sur ce bouton, rien ne se passe.
Quelqu'un peut-il s'il vous plaît m'aider avec comment puis-je afficher le texte en appuyant sur le bouton?
.xml
<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/txtView"
android:layout_width="150dp"
android:layout_height="150dp"
android:text="@string/hello" />
<Button
android:id="@+id/mybtn"
android:layout_width="50dp"
android:layout_height="30dp" />
<TextView
android:id="@+id/viewwidth"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/viewheight"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
Button mybtn;
TextView txtView;
String hello;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
mybtn= new Button(this);
txtView=new TextView(this);
mybtn.setOnClickListener(this);
printmyname();
mybtn = (Button)findViewById(R.id.mybtn);
txtView=(TextView)findViewById(R.id.txtView);
txtView = (TextView)findViewById(R.id.viewwidth);
txtView = (TextView)findViewById(R.id.viewheight);
hello="This is my first project";
//setContentView(mybtn);
// setContentView(R.layout.main);
}
public void onClick(View view){
txtView.setText(hello);
//printmyname();
Toast.makeText(NameonbuttonclickActivity.this, hello, Toast.LENGTH_LONG).show();
}
private void printmyname(){
System.out.println("coming");
}
}
j'ai travaillé thankyou..When j'ai utilisé les deux noms de vue du texte même, il travaillé. – suji
, pouvez-vous s'il vous plaît dites-moi quelle est la fonction de Toast.makeText (NameonbuttonclickActivity.this, "bonjour", Toast.LENGTH_LONG) .show(); – suji
Toast() créera simplement un message d'alerte sur l'écran pendant un certain temps, cela dépendra de la longueur que vous aurez donnée dans Toast(), mais aussi du contexte, de la chaîne à afficher et de la durée du toast – Dhruvisha