Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

displaying a string on the textview when clicking a button in android

I'm very new to Android development and just started to study.

What I'm trying is to add a button and when that button is pressed a text "my first project" to get displayed in the text view.

With the help of some experts I created the button and text view.

So the button is showing in the simulator but when I click that button nothing happens.

Can anyone please help me with how can I display the text when pressing the button?

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

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do like this:

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

Check your textview names. Both are same . You must use different object names and you have mentioned that textview object which is not available in your xml layout file. Hope this will help you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...