如何通过$ P $改变一个TextView的文本pssing一个按钮 [英] How to change a TextView's text by pressing a Button
本文介绍了如何通过$ P $改变一个TextView的文本pssing一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过pressing一个按钮
来改变一个的TextView
,但不明白如何正确地做到这一点。
这是我的布局的一部分:
< TextView的机器人:ID =@ + ID /计数器
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=文本/>
<按钮机器人:ID =@ + ID /按钮
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=更改文字! />
这是我的活动:
公共类点击延伸活动{
保护无效的onCreate(包冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.main);
最后Button按钮=(按钮)findViewById(R.id.button);
button.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(视图v){
//执行上的点击动作
// ???
}
});
}
}
我应该把里面的的onClick()
的方法?
解决方案
- 通过ID查找的TextView
- 通过调用更改文本
yourTextView.setText(新文本);
findViewById
和<一href="https://developer.android.com/reference/android/widget/TextView.html#setText%28java.lang.CharSequence%29"相对=nofollow> 的setText
的方法。
I want to change the of a TextView
by pressing a Button
, but don't understand how to do it properly.
This is part of my layout:
<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!" />
And this is my activity:
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
// ???
}
});
}
}
What should I put inside onClick()
method?
解决方案
- Find the textview by id
- Change the text by invoking
yourTextView.setText("New text");
Refer findViewById
and setText
methods.
这篇关于如何通过$ P $改变一个TextView的文本pssing一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文