【问题标题】:How can i get String from strings.xml in a class?如何从类中的 strings.xml 获取字符串?
【发布时间】:2020-11-12 12:01:00
【问题描述】:

我一直在尝试将此代码用于我的应用程序。我试图在 strings.xml 中调用这个类,但我想不通。如何调用strings.xml中的类?

公共类 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;
    }
}

【问题讨论】:

    标签: java android computer-science


    【解决方案1】:

    在这里您可以找到如何在 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>
    

    这里你可以找到如何从java调用

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

    【讨论】:

      【解决方案2】:

      您需要将所有字符串放在 string.xml 文件中,然后通过以下调用从您的代码中引用它们:

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

      【讨论】:

        猜你喜欢
        • 2011-11-14
        • 2012-09-20
        • 1970-01-01
        • 2016-12-02
        • 1970-01-01
        • 1970-01-01
        • 2011-11-30
        • 2015-09-25
        • 2016-10-23
        相关资源
        最近更新 更多