2011-07-16 108 views
10

我想通过按Button来更改TextView,但不知道如何正确执行。如何通过按下按钮来更改TextView的文本

这是我的布局的一部分:

<TextView android:id="@+id/counter" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Text" /> 
<Button android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Change text!" /> 

这是我的活动:

public class Click extends Activity { 
    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     final Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
       // ??? 
      } 
     }); 
    } 
} 

我应该把里面onClick()方法?

回答

12
  1. 查找ID
  2. 更改文本TextView的调用yourTextView.setText("New text");

参考findViewByIdsetText方法。

+0

一个重要的t hing:只有当您离开onClick块时才更新该字段,但无法两次更新相同的字段。 – onizukaek

3
TextView tv = (TextView) v; 
tv.setText("My new text"); 

编辑: 这里是您的处理程序:

button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      //TextView tv = (TextView) v; //code corrected 
      TextView tv= (TextView) findViewById(R.id.counter); 
      tv.setText("My new text"); 
     } 
}); 

TextView的视图=(TextView的)findViewById(R.id.counter);

+0

我希望你知道这是完全错误的。 'onClick()'方法中的'v'参数是点击的视图。现在,如果他用你的代码点击一个'Button',所有发生的事情就是'Button'的文本改变了。 –

+0

哦! Button和TextView是不同的。是的,这是我的错误。我要删除我的答案。 – Vikas

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

public class Click extends Activity { 
int i=0; 
    protected void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     final TextView mTextView = (TextView) findViewById(R.id.counter) 
     mTextView.setText("hello "+i); 

     final Button button = (Button) findViewById(R.id.button); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
       i=i+1; 
       mTextView.setText("hello "+i); 
      } 
     }); 
    } 
} 

希望这会成为你的需要

+0

谢谢。此代码有效。 – Roman

+0

欢迎..什么投票人 –

+0

我试过这个,我修改这样的'mTextView.setText(Integer.toString(i));',它的工作。谢谢。 +1 – InnocentKiller

0

你可以用的setText( “什么”)方法做到这一点。

0

onclick,采取TextView的对象,并设置所需的文字,像这样:

tvOBJECT.setText("your text"); 
0

的Java只没有XML版本

为了使事情变得更加明确:

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

public class Main extends Activity { 
    private int i; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     this.i = 0; 

     final TextView tv = new TextView(this); 
     tv.setText(String.format("%d", this.i)); 

     final Button button = new Button(this); 
     button.setText("click me"); 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Main.this.i++; 
       tv.setText(String.format("%d", Main.this.i)); 
      } 
     }); 

     final LinearLayout linearLayout = new LinearLayout(this); 
     linearLayout.addView(button); 
     linearLayout.addView(tv); 
     this.setContentView(linearLayout); 
    } 
} 

在Android上测试22.

相关问题