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

后端 未结 9 1968
情深已故
情深已故 2020-12-10 08:43

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 ge

相关标签:
9条回答
  • 2020-12-10 09:02

    Check this:

    hello.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View paramView) {
            text.setText("hello");
        }
    });
    
    0 讨论(0)
  • 2020-12-10 09:04

    First create xml file as follows. Create one textview and a button:

    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>
    

    The first TextView is created by default. You can leave or remove it if you want. Next one is to create a button The next one is TextView where you want to display text.

    Now coming to the main activity code... 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 讨论(0)
  • 2020-12-10 09:04

    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>
    
    0 讨论(0)
  • 2020-12-10 09:08

    Just check your code in .java class

    You had written below line

     mybtn.setOnClickListener(this);
    

    before initializing the mybtn object I mean

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

    just switch this two line or put that line "mybtn.setOnClickListener(this)" after initializing your mybtn object and you will get the answer what you want..

    0 讨论(0)
  • 2020-12-10 09:14

    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.

    0 讨论(0)
  • 2020-12-10 09:14

    This is because you DON'T associated the OnClickListener() on the button retrieve from the XML layout.

    You don't need to create object because they are already created by the Android system when you inflate XML file (with the Activity.setContentLayout( int resourceLayoutId ) method).

    Just retrieve them with the findViewById(...) method.

    0 讨论(0)
提交回复
热议问题