java 在 Android 视图中显示动态文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6821444/
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-10-30 17:24:11  来源:igfitidea点击:

Display dynamic text in an Android view

javaandroidandroid-layout

提问by sirmdawg

I'm pretty new to developing Android applications but I am attempting to display the score at the end of a game I am making.

我对开发 Android 应用程序还很陌生,但我试图在我制作的游戏结束时显示分数。

I was planning on switching to a view using:

我打算使用以下方法切换到视图:

setContentView(R.layout.over);

And that seems to work but I want to display the score.

这似乎有效,但我想显示分数。

I defined a textView:

我定义了一个文本视图:

<TextView android:text="@+id/TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

But would like to change the text to read: "Score: (dynamic text from variable here)". Obviously I need to reference that variable somehow, or set the text of the textView before changing to this view.

但是想将文本更改为:“分数:(此处变量的动态文本)”。显然,我需要以某种方式引用该变量,或者在更改为该视图之前设置 textView 的文本。

Please help!

请帮忙!

采纳答案by gregory561

That should solve your problem:

那应该可以解决您的问题:

TextView tv = (TextView)findViewById(R.id.TextView02);  
tv.setText("Text to set");

Why do u assign to text and id the same values?

为什么你给 text 和 id 分配相同的值?

回答by Robby Cohen

Greg's Answer's pretty much what you want, but customized to what you are looking for:

Greg's Answer 几乎是您想要的,但根据您的需求进行了定制:

 TextView tv = (TextView)findViewById(R.id.TextView02);
 tv.setText("Score: "+scoreVariable);

回答by JAiro

I should use a dialog windows to show the Scrore. here ypou have how to create differents dialogs.

我应该使用一个对话框窗口来显示 Scrore。这里你有如何创建不同的对话框。

http://developer.android.com/guide/topics/ui/dialogs.html

http://developer.android.com/guide/topics/ui/dialogs.html