如何根据数组中的位置使用一个字符串数组条目(Android)?
我正在尝试制作一个简单的预言机应用程序,当您向应用程序提出问题时,它会根据随机数给出答案。
我做了一些研究,在我看来,最好的做法是为我的答案创建一个包含字符串数组的资源,然后我可以在数组中输入我的各种答案。
在我的主要活动的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的代码将把你的字符串资源数组放入一个名为answers的String[]中。
然后,您可以通过仅供参考选择答案
,资源中的字符串数组只能有 512 个条目长。
一些字符串资源帮助
一些常规资源类帮助
The below code will put your String resource array into a String[] called answers.
You can then select your answer with
An FYI, string arrays in resources can only be 512 entries long.
Some string resource help
Some general resource class help