eclipse 将文本设置为变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7060976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:50:40  来源:igfitidea点击:

setText to a variable

javaandroideclipse

提问by user888712

I'm having trouble creating a simple app, I created a variable that changes when you click a button, but I would like to know how to set my TextView to that variable.

我在创建一个简单的应用程序时遇到了问题,我创建了一个在您单击按钮时会发生变化的变量,但我想知道如何将我的 TextView 设置为该变量。

total is my TextView, and count is my variable.

total 是我的 TextView,count 是我的变量。

I am trying total.setText(count);

我正在尝试 total.setText(count);

I dont know how to tell it to just take the value of count and set the text to that.

我不知道如何告诉它只取计数值并将文本设置为该值。

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by Mike

First, you need to give your TextView an ID in the layout file:

首先,您需要在布局文件中为您的 TextView 提供一个 ID:

<TextView
    <!-- existing code -->
    android:id="@+id/total">
</TextView>

Then use something like this to get the instance of the TextView and set the text:

然后使用这样的东西来获取 TextView 的实例并设置文本:

TextView total = (TextView) findViewById(R.id.total);
total.setText(Integer.toString(count));

回答by Hyman

Use:

用:

total.setText(String.valueOf(count));

回答by Amir Emad

Well, you can create a Global Variable

那么,你可以创建一个 Global Variable

and

Modify its value on Button pressand then call the value in the TextView

修改其值Button press,然后调用TextView

I hope this helps you

我希望这可以帮助你

回答by Carlos Silva

Huuummmm

呼噜噜

total.setText(String.valueOf(count));

total.setText(String.valueOf(count));

That should do it.

那应该这样做。

回答by Asaph

Assuming countis an int, and your TextViewis an instance of android.widget.TextView:

假设countint,而您TextView是 的一个实例android.widget.TextView

total.setText(Integer.toString(count));

setText()takes an argument of type CharSequenceand so an intneeds to be converted to an object type that implements the CharSequenceinterface. Stringis the obvious choice.

setText()需要一个类型的参数,CharSequence因此int需要将其转换为实现该CharSequence接口的对象类型。String是显而易见的选择。