创建字符串可以用作Filepath - Eclipse / Android [英] Creating Strings than can be used as Filepath - Eclipse / Android

查看:106
本文介绍了创建字符串可以用作Filepath - Eclipse / Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更多代码 - 这里创建的字符串是 R.string.c #### - c。#### 是一个预定义的字符串,我想以一个setText。

More Code - The String Created here is R.string.c#### - c.#### is a pre-defined string i want to run as a setText.

int Q1 = question1.getmCounter();
int Q2 = question2.getmCounter();
int Q3 = question3.getmCounter();
int Q4 = question4.getmCounter();

int qTotal = Q1 + Q2 + Q3 + Q4;
String Test5 = "R.string.c" + qTotal;

这是我现在得到我要饲养的字符串。 b
$ b

This is how i am now getting the "string" i want to feed.

    textOut = (TextView) findViewById(R.id.ChmpNametxt);
    textOut.setText(Test5);

这是我想要喂它的地方。

This is where i want to feed it.

推荐答案

您可以使用getIdentifier方法访问资源。它允许访问包的任何资源:

You can access to resources by using the "getIdentifier" method. It allows to access any resource of the package:

getResources().getIdentifier("c1123", "string", this.class.getPackageName());

您可以传递第一个参数中的任何变量,这对应于字符串的名称。
第二个参数指定搜索哪个资源(string,drawable,layout .....)。
第三个参数是应用程序的包名称。它指定使用哪个R。在你的情况下,你想查看应用程序中包含的资源(R)。

You can pass any variable in the first argument, this corresponds to the name of your string. The second argument specifies in which resources to search it (string, drawable, layout.....). The third argument is the application's package name. It specifies which "R" is used. In your case, you want to look inside the resources ("R") contained in your application.

编辑:此方法返回一个int,与您正在寻找的资源,例如R.string.c1123

This method returns an int, that correspond to the ID of the resource you're looking for, for exemple R.string.c1123

Edit2:此int应该用作setText函数的参数。

This int should then be used as the parameter for the setText function.

所以完整的代码将是:

int resId = getResources().getIdentifier("c" + qTotal, "string", this.class.getPackageName());
textOut.setText(resId);

这篇关于创建字符串可以用作Filepath - Eclipse / Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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