在Android中点击按钮时,在TextView上显示字符串

5
我对Android开发非常陌生,刚开始学习。我正在尝试添加一个按钮,当按下该按钮时,文本“我的第一个项目”将显示在文本视图中。在一些专家的帮助下,我创建了按钮和文本视图。因此,该按钮在模拟器中显示,但当我点击该按钮时,什么也没有发生。请问有谁能告诉我如何在按下按钮时显示文本呢?.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");       

    }
}
9个回答

9
你可以像这样做:

你可以这样做:

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

请检查您的TextView名称。两个名称是相同的。您必须使用不同的对象名称,并且您已经提到了一个在您的XML布局文件中不存在的TextView对象。 希望这可以帮助你。

我已经工作了,谢谢。当我使用相同的文本视图名称时,它起作用了。 - suji
请问您能告诉我 Toast.makeText(NameonbuttonclickActivity.this, "hello", Toast.LENGTH_LONG).show() 的作用是什么吗? - suji
Toast() 只会在屏幕上创建一个警报消息,时间取决于您在 Toast() 中设置的长度。它需要上下文、要显示的字符串和 toast 的时长。 - Dhruvisha
@druvisha.ok. 如果我想要更改按钮的标题,我该怎么做呢?你能帮我吗? - suji
创建一个按钮对象,假设它的名字是mybtn。你可以在Java文件中写入mybtn.setText("新按钮名称")这一行代码。在XML布局文件中,你可以添加属性android:text="按钮名称"。 - Dhruvisha
愿你的面容充满成功:你帮了很多忙。 - Humphrey

3
请先创建以下xml文件,其中包含一个文本框和一个按钮:

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>

默认情况下会创建第一个TextView,如果您想要可以保留或删除它。 接下来是创建按钮 下一个是TextView,您希望在其中显示文本。

现在转到主Activity代码... 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");

        }         });
    }
}

非常感谢你的帮助,希望将来还能得到你的帮助。 - suji

2

请检查你的.java类中的代码

你写了以下这行代码

 mybtn.setOnClickListener(this);

在初始化mybtn对象之前,我是指

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

只需要交换这两行代码或将"mybtn.setOnClickListener(this)"这行代码放在初始化mybtn对象的后面,你就能得到想要的答案。


现在通常都会按照以下步骤进行编程:首先,初始化所有对象,然后将监听器或任何属性设置到它上面。如果我的回答对您有帮助,请接受为答案.. :) - Dhrumil Shah - dhuma1981

2
这是因为你没有在从XML布局检索到的按钮上关联OnClickListener()。
不需要创建对象,因为当您用Activity.setContentLayout(int resourceLayoutId)方法填充XML文件时,它们已经被Android系统创建。
只需使用findViewById(...)方法检索它们即可。

1

试试这个

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

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

在 toast 中也使用 "Hello"。


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

请检查:

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

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>

0

你可以这样使用:txtView.setText("你好");


当点击按钮时仍然没有任何反应。 - suji
在XML中为按钮添加可点击属性。 - Mal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接