更改 TextView 文本 [英] Change TextView text
本文介绍了更改 TextView 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从代码中更改我的 TextView
文本.
I'm trying to change my TextView
text from the code.
这就是我的 xml 的样子:
This is what my xml looks like:
XML:
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal" />
还有代码:
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
setContentView(tv1);
我的设备出现错误,应用程序停止.我试图显示一个 TextView
(未连接到 XML TextView
)并且它起作用了.
I'm getting an error on my device and the application stops.
I tried to show a TextView
(not connected to an XML TextView
) and it worked.
推荐答案
你的方法不对.我认为这将是空指针异常(下次发布日志猫)
Your approach is incorrect. I think it will be Null Pointer Exception (next time post log cat)
在查找视图之前,您需要先指定要使用的布局.
You need to specify the layout you are using first, before finding views.
Java:
// Specify the layout you are using.
setContentView(R.layout.yourlayout);
// Load and use views afterwards
TextView tv1 = (TextView)findViewById(R.id.textView1);
tv1.setText("Hello");
科特林:
// Specify the layout you are using.
setContentView(R.layout.yourlayout)
// Load and use views afterwards
val tv1: TextView = findViewById(R.id.textView1)
tv1.text = "Hello"
点击这里来研究你想知道
这篇关于更改 TextView 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文