简体   繁体   English

如何从 class 中的 strings.xml 获取字符串?

[英]How can i get String from strings.xml in a class?

i have been trying to use this code for my application.我一直在尝试将此代码用于我的应用程序。 Im trying to call this class in strings.xml but i couldn't figure it out.我试图在 strings.xml 中调用这个 class 但我无法弄清楚。 How can i call the class in strings.xml?我如何在 strings.xml 中调用 class?

public class Sorular {公共 class Sorular {

    public String Sorular[] = {
            "When did Facebook came out?",
            "Who is a Google Developer?",
            "Whitch one is the first Smartphone?"
    };

    public String Secenekler[][] = {
            {"2002", "2003", "2004"},
            {"Bill Gates", "Steve Jobs", "Larry Page"},
            {"Iphone", "Samsung", "Nokia"}
    };

    public String DogruCevap[] = {
            "2004",
            "Larry Page",
            "Iphone"
    };

    public String getQuestion ( int a){
        String soru = Sorular[a];
        return soru;
    }

    public String getchoice1 ( int a){
        String secenek = Secenekler[a][0];
        return secenek;
    }

    public String getchoice2 ( int a){
        String secenek = Secenekler[a][1];
        return secenek;
    }

    public String getchoice3 ( int a){
        String secenek = Secenekler[a][2];
        return secenek;
    }

    public String getCorrectAnswer ( int a){
        String cevap = DogruCevap[a];
        return cevap;
    }
}

Here you find how to add array in string.xml在这里您可以找到如何在 string.xml 中添加数组

<string-array name="Sorular">
    <item>When did Facebook came out?</item>
    <item>Who is a Google Developer?</item>
    <item>Whitch one is the first Smartphone?</item>
</string-array>

Here you can find how to call from java在这里您可以找到如何从 java 拨打电话

public String getQuestion (Context context, int a){
    String soru = context.getResources().getStringArray(R.array.Sorular)[a];
    return soru;
}

You need to put all your strings inside string.xml file, and then reference them from your code with the following call:您需要将所有字符串放入 string.xml 文件中,然后通过以下调用从您的代码中引用它们:

context.getResources().getString(R.string.word);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM