如何访问随机字符串。xml

时间:2020-12-24 23:59:04

A simple Quote generator is in question, i have conceptualized the code and have presumed it would work if i keep my Strings in xml

一个简单的引号生成器有问题,我对代码进行了概念化,并假定如果我将字符串保存在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 * 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." -* 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>

and access them in the main.

并进入他们的主要。

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);
            }
        });

    }

I guess my question is more: How could one access the String.xml value trough a String variable. I find that this might be useful in the future. For now I've opted out to do this in a separate class trough an Array.

我想我的问题更多的是:一个人如何访问这个字符串。通过字符串变量获取xml值。我发现这在将来可能是有用的。现在,我选择在一个单独的类中通过一个数组来完成这个操作。

3 个解决方案

#1


5  

Define your quotes in a string-array in strings.xml as below

在字符串中的字符串数组中定义引号。xml作为下面

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

Then, access to this resource in your activity/fragment/whatelse

然后,访问您的活动/片段/其他资源

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

#2


1  

Use getResources().getIdentifier("s1", "string", Activity.this.getPackageName()) to get resource id. Then call txt.setText(id)

使用getresource()。getIdentifier("s1"、"string"、Activity.this.getPackageName()))用于获取资源id。

#3


1  

Try this:

试试这个:

Let's say that you want this string name:

假设你想要这个字符串名:

final String ss1 = "s1";

You can get the string with that name (dynamically as string), as follows:

您可以获得具有该名称的字符串(动态地作为字符串),如下所示:

final String x = getString(getApplicationContext().getResources().getIdentifier(ss1, "string", getPackageName()));

#1


5  

Define your quotes in a string-array in strings.xml as below

在字符串中的字符串数组中定义引号。xml作为下面

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

Then, access to this resource in your activity/fragment/whatelse

然后,访问您的活动/片段/其他资源

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

#2


1  

Use getResources().getIdentifier("s1", "string", Activity.this.getPackageName()) to get resource id. Then call txt.setText(id)

使用getresource()。getIdentifier("s1"、"string"、Activity.this.getPackageName()))用于获取资源id。

#3


1  

Try this:

试试这个:

Let's say that you want this string name:

假设你想要这个字符串名:

final String ss1 = "s1";

You can get the string with that name (dynamically as string), as follows:

您可以获得具有该名称的字符串(动态地作为字符串),如下所示:

final String x = getString(getApplicationContext().getResources().getIdentifier(ss1, "string", getPackageName()));