如何更改在Java code TextView的价值? [英] How do I change TextView Value inside Java Code?

查看:108
本文介绍了如何更改在Java code TextView的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android程序。用户点击一个按钮,我做一些数学,我想改变,我有我的一些TextView的对象视图中的值。是否有人可以告诉我怎么做,我的code?

I am working on a android program. A user clicks on a button I do some math and I would like to change the values that I have on my view in some TextView objects. Can someone please tell me how to do it in my code?

推荐答案

我presume,这个问题是一个延续的这个

I presume that this question is a continuation of this one.

你到底想干什么?难道你真的想动态改变文本的TextView的对象,当用户点击一个按钮?你当然可以这样做,如果你有一个原因,但是,如果文本是静态的,它通常设置在main.xml中的文件,像这样的:

What are you trying to do? Do you really want to dynamically change the text in your TextView objects when the user clicks a button? You can certainly do that, if you have a reason, but, if the text is static, it is usually set in the main.xml file, like this:

<TextView  
android:id="@+id/rate"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/rate"
/>

字符串@字符串/率是指在你的strings.xml文件,看起来像这样的条目:

The string "@string/rate" refers to an entry in your strings.xml file that looks like this:

<string name="rate">Rate</string>

如果你真的想以后更改本文中,您可以通过使用尼古拉的例子做 - 你会得到一个参考的TextView利用中main.xml中定义的ID,像这样的:

If you really want to change this text later, you can do so by using Nikolay's example - you'd get a reference to the TextView by utilizing the id defined for it within main.xml, like this:


final TextView textViewToChange = (TextView) findViewById(R.id.rate);
textViewToChange.setText(
    "The new text that I'd like to display now that the user has pushed a button.");

这篇关于如何更改在Java code TextView的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆