2012-02-08 2 views
3

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

    } 
} 

Répondre

7

Vous pouvez faire comme ceci:

String hello; 
public void onCreate(Bundle savedInstanceState) { 
    setContentView(R.layout.main); 
    super.onCreate(savedInstanceState); 

    mybtn = (Button)findViewById(R.id.mybtn); 
    txtView=(TextView)findViewById(R.id.txtView); 
    txtwidth = (TextView)findViewById(R.id.viewwidth); 
    hello="This is my first project"; 


    mybtn.setOnClickListener(this); 
} 
public void onClick(View view){ 
    txtView.setText(hello); 
} 

Vérifiez vos noms de TextView. Les deux sont identiques . Vous devez utiliser des noms d'objet différents et vous avez mentionné cet objet textview qui n'est pas disponible dans votre fichier de mise en page XML. J'espère que cela vous aidera.

+0

j'ai travaillé thankyou..When j'ai utilisé les deux noms de vue du texte même, il travaillé. – suji

+0

, pouvez-vous s'il vous plaît dites-moi quelle est la fonction de Toast.makeText (NameonbuttonclickActivity.this, "bonjour", Toast.LENGTH_LONG) .show(); – suji

+0

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

0

vous utilisez ce que txtView.setText("hello");

+0

, toujours rien ne vient lorsque le bouton est cliqué – suji

+0

ajouter la propriété cliquable pour le bouton dans xml – Mal

0

Essayez cette

public void onClick(View view){ 
    txtView.setText("hello"); 

    //printmyname(); 
    Toast.makeText(NameonbuttonclickActivity.this, "hello", Toast.LENGTH_LONG).show(); 
} 

également utilisé toast "Bonjour"

0
public void onCreate(Bundle savedInstanceState) 
{ 
    setContentView(R.layout.main); 
    super.onCreate(savedInstanceState); 

    mybtn = (Button)findViewById(R.id.mybtn); 
    txtView=(TextView)findViewById(R.id.txtView); 

    mybtn .setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      txtView.SetText("Your Message"); 
     } 
    }); 
} 
0

Vérifiez ceci:

hello.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View paramView) { 
     text.setText("hello"); 
    } 
}); 
3

d'abord créer un fichier xml comme suit. Créer un textview et un bouton:

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<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:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" /> 

    <Button 
    android:id="@+id/mybutton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Click Me" /> 

    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 

</LinearLayout> 

Le premier TextView est créé par défaut. Vous pouvez le quitter ou le supprimer si vous le souhaitez. Le suivant est de créer un bouton Le prochain est TextView où vous voulez afficher le texte.

En arrivant au code d'activité principal ... package com.android.example.simple;

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class SimpleActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final TextView textView=(TextView)findViewById(R.id.textView1); 
    final Button button1 = (Button)findViewById(R.id.mybutton); 

    //Implement listener for your button so that when you click the 
    //button, android will listen to it.    

    button1.setOnClickListener(new View.OnClickListener() {    
     public void onClick(View v) {     
     // Perform action on click 
      textView.setText("You clicked the button"); 

     }   }); 
    } 
} 
+0

merci beaucoup friend.it aidé et j'espère pour votre aide à l'avenir aussi – suji

2

Ceci est parce que vous DO NOT associé le OnClickListener() sur le bouton récupérer à partir de la mise en page XML.

Vous n'avez pas besoin de créer d'objet car ils sont déjà créés par le système Android lorsque vous gonflez le fichier XML (avec la méthode Activity.setContentLayout (int resourceLayoutId)). Il suffit de les récupérer avec la méthode findViewById (...).

2

Vérifiez votre code.classe java

Vous aviez écrit ci-dessous ligne

mybtn.setOnClickListener(this); 

avant d'initialiser l'objet myBtn Je veux dire

mybtn = (Button)findViewById(R.id.mybtn); 

juste passer cette ligne deux ou mettre cette ligne "mybtn.setOnClickListener (this)" après l'initialisation de votre objet mybtn et vous obtiendrez la réponse que vous voulez ..

+0

merci mec pour votre information – suji

+0

maintenant toujours ceci suit ces étapes qui d'abord initialisent tout l'objet et puis mettent l'écouteur ou n'importe quelle propriété à lui .. et si ma réponse est utile pour vous alors acceptez s'il vous plaît comme réponse .. :) – dhuma1981

0

MainActivity.java:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.view.View; 
import android.view.View.OnClickListener; 

public class MainActivity extends Activity { 

    Button button1; 
    TextView textView1; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button1=(Button)findViewById(R.id.button1); 
     textView1=(TextView)findViewById(R.id.textView1); 
     button1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

      textView1.setText("TextView displayed Successfully"); 

      } 
     }); 

} 

} 

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

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

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

</LinearLayout> 
Questions connexes