如何显示在XML活动整数? [英] How to show integer from activity in XML?

查看:136
本文介绍了如何显示在XML活动整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用XML输出​​在我的应用程序。所以基本上主要活动只是告诉了android显示主要的XML布局。

I use XML output in my app. So basically the main activity just tells the android to show the XML layout of main.

但是,如果我在定义整型变量活动code,我想这个整型变量,也可以在显示器上显示?

But what if I have in the activity code defined integer variable and I want this integer variable also be shown on the display?

我如何推整型变量到XML?从主XML参考XML其他字符串很简单 - @字符串/ APP_NAME ...但我怎么使用整型变量从活动

How do I PUSH the integer variable to the XML??? From main XML reference to other strings in XML is easy - @string/app_name ... but how do I use the integer variable from the activity?

推荐答案

presumably你展示一些文字的TextView

Presumably you're showing some text in a TextView.

您可以显示从字符串资源文本(在XML定义,被称为 R.string。* ,你提到),或者从字符串在运行时。

You can display text either from a string resource (defined in XML and referred to as R.string.*, as you mention) or from a String at runtime.

您不能在运行时更改的XML资源;将其用于如标签或其他UI文本固定值。因此,有没有办法来推的值,以XML。

You can't change the XML resources at runtime; you use them for fixed values like labels or other UI text. So there's no way to "push" a value to XML.

但是,你可以愉快地做这样的事情在运行时,动态地更新用户界面:

But you can happily do something like this at runtime, dynamically updating your UI:

int userAge = calculateUsersAge();
TextView age = (TextView) findViewById(R.id.age_field);
age.setText(userAge +" years old");

或者更好,确保没有硬codeD值在code:

Or better, ensuring there are no hardcoded values in the code:

age.setText(getString(R.string.years_old, userAge));

其中, years_old 是文本%d个岁的在你的 RES /价值/ strings.xml中和%d个雅雷ALT在你的 RES /值-DE / strings.xml中,等等。

Where years_old is the text "%d years old" in your res/values/strings.xml and "%d Jahre alt" in your res/values-de/strings.xml, and so on.

这篇关于如何显示在XML活动整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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