Android SDK 单击按钮时更改文本

标签 android button sdk

 package com.purelymean.earnings;
 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.Button;
 import android.widget.TextView;

 public class Main extends Activity{
     /**Called when activity is first created. */
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         setContentView(R.layout.main);
         Button b = (Button) findViewById(R.id.button);
         TextView tx = (TextView) findViewById(R.id.textView1);
     }
 }

我需要放置什么,以便单击按钮时会将 textview 更改为我放置的任何内容?

最佳答案

您应该向按钮添加一个 OnClickListener 并重写 onClick 函数以在 TextView 中设置所需的文本

Button b = (Button) findViewById(R.id.button);

b.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
       TextView tx = (TextView) findViewById(R.id.textView1);
       tx.setText("yourtext");
   }
});

关于Android SDK 单击按钮时更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11486715/

相关文章:

azure - Visual Studio 2013 上的错误 160 WAT080 : Failed to locate the Windows Azure SDK 2. 3

android - 有没有办法在 MPAndroidChart 的条形图上隐藏每个条形图的值标签?

android - 在哪里可以查看Android系统中进程生命周期管理的android源码?

Javascript - 使用 If 语句检查按钮点击

android - ExpandableListView 中按钮的 onClick 方法

java - Android Studio 找不到 SDK

android - 我对 pdftron 集成有疑问

android - YouTubePlayerSupportFragment 监听器不工作

java - RxJava/RxAndroid 检查互联网连接是否可用

android - 具有两个工作 Activity 的单个按钮