如何在外部类中使用getString(R.string)?

时间:2015-08-17 23:57:04

标签: android

如何在外部课程中使用$_virtual

我想知道如何从外部类中的字符串getString(R.string)获取值。感谢。

3 个答案:

答案 0 :(得分:0)

做这样的事情:

Java代码:

String string = getString(R.string.hello);

的strings.xml:

<resources>
    <string name="hello">Hello!</string>
</resources>

答案 1 :(得分:0)

你可以这样做:

public static class OtherClass{

    public static void getString(Context mContext){
        String str = mContext.getString(R.string.app_name);
        Log.i("String value", str);
    }

}

如何从活动

致电
OtherClass.getString(this);

希望这会对你有所帮助。

答案 2 :(得分:0)

您需要上下文才能在任何类中使用let Page = require('./../page') let JsTestingPage = Object.create(Page, { firstLevelFunction1: {get: function () { return 'first level function1' }}, firstLevelFunction2: {get: function () { return 'first level function2' }}, firstLevelGroup: { get: function () { return { secondLevelFunction1: { get: function () { return 'second level function1' } }, secondLevelFunction2: { get: function () { return 'second level function2' } } } } }, testLevels: {value: function () { console.log(this.firstLevelFunction1) console.log(this.firstLevelFunction2) console.log(this.firstLevelGroup.secondLevelFunction1) console.log(this.firstLevelGroup.secondLevelFunction2) }} }) module.exports = JsTestingPage 方法。

制作一个MyApplication.java类文件。

getString(..)

在清单文件的public class MyApplication extends Application { private static Context context; @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); } public static Context getContext() { return context; } } 标签中添加name参数,例如,

Application

然后在您可以使用以下任何类的任何行中使用<application android:name=".MyApplication" android:icon="@mipmap/ic_launcher" .... 方法。

getString(..)