java - 我如何访问随机 string.xml

标签 java android xml string findviewbyid

一个简单的报价生成器有问题,我已经概念化了代码并假设如果我将我的字符串保存在 xml 中它会工作

<resources>
    <string name="app_name">"Lift Quote"</string>
    <string name="action_settings">Settings</string>
    <!-- put buttons in here -->
    <!-- here are the quotes -->
    <!-- inspiring -->
    <string name="s1">"He who has a why to live can bear almost any how." -Nietzche</string>
    <string name="s2">"Life is about making an impact, not making an income." –Kevin Kruse</string>
    <string name="s3">You miss 100% of the shots you don’t take. –Wayne Gretzky</string>
    <!-- deep -->
    <string name="s4">" A man's character is his fate. " -Heraclitus</string>
    <string name="s5">"Why do you stay in prison when the door is so wide open?" -Rumi</string>
    <string name="s6">"When I let go of what I am, I become what I might be." -Lao Tzu</string>
    <!-- positive -->
    <string name="s8">"Happiness is not something ready made. It comes from your own actions." -Dalai Lama</string>
    <string name="s9">"If you have a positive attitude and constantly strive to give your best effort,
        eventually you will overcome your immediate problems and find you are ready for greater challenges." -Pat Riley
</string>

</resources>

并在主目录中访问它们。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        // WHERE MY CODE STARTS

        //RANDOM GENERATOR that DOESN'T WORK

        Random rand = new Random();

        final int irandomIndex = rand.nextInt((3 - 1) + 1)+1;
        final int drandomIndex = rand.nextInt((4 - 6) + 1)+4;
        final int prandomIndex = rand.nextInt((7 - 9) + 1)+7;

        final String iIndex = "s"+irandomIndex;
        final String dIndex = "s"+drandomIndex;
        final String pIndex = "s"+prandomIndex;



        //STRING

        final TextView txt = (TextView) findViewById(R.id.textView);

        //BUTTONS
        //

        Button inspireBtn = (Button) findViewById(R.id.iButton);
        Button deepBtn = (Button) findViewById(R.id.dButton);
        Button positiveBtn = (Button) findViewById(R.id.pButton);

        // ON CLICK LISTENRS


        inspireBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                String index = iIndex;
                txt.setText(getResources().getString(R.string.index));
                                                //"cannot resolve symbol 'index';"
            }
        });



        //this wouldn't work either
        final String ss1 = "s1";
        final String x = getResources().getString(R.string.ss1);
        deepBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                txt.setText(x);
            }
        });


        //this wouldn't work either same error
        final String sx2 = "s1";
        final String m = getResources().getString(R.string.sx2);
        positiveBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                txt.setText(m);
            }
        });

    }

我想我的问题更多:如何通过 String 变量访问 String.xml 值。我发现这在将来可能会有用。 现在,我选择不通过数组在单独的类中执行此操作。

最佳答案

在 strings.xml 中的 string-array 中定义引号,如下所示

<string-array name="mystrings">
        <item>value 1</item>
        <item>value 2</item>
        <item>value 3</item>
</string-array>

然后,在你的 activity/fragment/whatelse 中访问这个资源

String[] array = getResources().getStringArray(R.array.mystrings);

关于java - 我如何访问随机 string.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36094033/

相关文章:

java - HashMap 删除不起作用

java - HQL 查询在运行 SQL 查询时抛出 QueryException

XML Xpath - 选择其中元素 = 多个值

java - 简单XML : How to read XML content as list?

Java:计算星期几适用于除本月之外的所有日期

java - 在低端笔记本电脑上进行 Java 编程的好的 IDE 是什么?

带有可滚动页脚的 Android 抽屉导航(菜单)

java - 在 OnCreate() 方法中绑定(bind)服务时出现 NullPointerException

java - Gson - 从 Json 转换为类型化的 ArrayList<T>

java - 构建json相关问题