如何根据数组中的位置使用一个字符串数组条目(Android)?

发布于 2024-11-01 02:52:16 字数 2076 浏览 3 评论 0原文

我正在尝试制作一个简单的预言机应用程序,当您向应用程序提出问题时,它会根据随机数给出答案。

我做了一些研究,在我看来,最好的做法是为我的答案创建一个包含字符串数组的资源,然后我可以在数组中输入我的各种答案。

在我的主要活动的 Java 代码中,我可以通过单击按钮生成一个随机数。该随机数然后可以对应于索引的编号。

当我尝试在代码中将随机数和访问字符串数组的工具拼凑在一起时,我的问题就出现了。

这是我的answerStrings.xml 文件的副本

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string-array
    name="answers">
    <item>Yes</item>
    <item>No</item>
    <item>Maybe</item>
    <item>Quite possibly</item>
</string-array>
</resources>

这是我的java 代码:

import java.util.Random;

import android.app.Activity;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

TextView    outputText;
MediaPlayer mpGobble;
Vibrator    vibr;
String[]    answers;
private Random myRandom = new Random();
int         randomNumber;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //find the output text view for use within the activity

    outputText = (TextView)findViewById(R.id.textView01);

    outputText.setOnClickListener(this);

    //Declare an array of outputs
    Resources res = getResources();
    String[] answers = res.getStringArray(R.array.answers);
    //--------^ Problem

    outputText.setText("Ask your question then click me");
}


@Override
public void onClick(View src) {
    switch(src.getId()){
    case R.id.textView01:

        //creates a random number
        int randomNumber = myRandom.nextInt(3);

        String answer = answers[randomNumber];

        outputText.setText(answer);




            break;
    }

}
}

我正在使用Eclipse IDE 使用Java 进行编程,正如您可能已经猜到的那样,我是这个游戏的新手!

非常感谢所有帮助!

I'm trying to produce a simple oracle app where when you ask the app a question it spits out an answer based on a random number.

I've done some research and it seems to me the best practice way of doing things is to create a resource for my answers containing a string array, i can then type my various answers in to the array.

In my Java code in my main activity i can then generate a random number upon the click of a button. This random number can then correspond to the number of the index.

My problem comes when i try to piece the random number and the facility to access the string array together in code.

Here is a copy of my answerStrings.xml file

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string-array
    name="answers">
    <item>Yes</item>
    <item>No</item>
    <item>Maybe</item>
    <item>Quite possibly</item>
</string-array>
</resources>

Here is my java code:

import java.util.Random;

import android.app.Activity;
import android.content.res.Resources;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

TextView    outputText;
MediaPlayer mpGobble;
Vibrator    vibr;
String[]    answers;
private Random myRandom = new Random();
int         randomNumber;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //find the output text view for use within the activity

    outputText = (TextView)findViewById(R.id.textView01);

    outputText.setOnClickListener(this);

    //Declare an array of outputs
    Resources res = getResources();
    String[] answers = res.getStringArray(R.array.answers);
    //--------^ Problem

    outputText.setText("Ask your question then click me");
}


@Override
public void onClick(View src) {
    switch(src.getId()){
    case R.id.textView01:

        //creates a random number
        int randomNumber = myRandom.nextInt(3);

        String answer = answers[randomNumber];

        outputText.setText(answer);




            break;
    }

}
}

I'm programming in Java with the eclipse IDE and as you have probably guessed i'm new to the game!

All help is much appreciated!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唔猫 2024-11-08 02:52:16

下面的代码将把你的字符串资源数组放入一个名为answers的String[]中。

Resources res = getResources();
String[] answers = res.getStringArray(R.array.answers);

然后,您可以通过仅供参考选择答案

String answer = answers[YOUR_RANDOM_WHOLE_NUMBER];

,资源中的字符串数组只能有 512 个条目长。
一些字符串资源帮助
一些常规资源类帮助

The below code will put your String resource array into a String[] called answers.

Resources res = getResources();
String[] answers = res.getStringArray(R.array.answers);

You can then select your answer with

String answer = answers[YOUR_RANDOM_WHOLE_NUMBER];

An FYI, string arrays in resources can only be 512 entries long.
Some string resource help
Some general resource class help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文