我认为这条线是问题所在
// 一个项目被选中。
int roll = DICE[spinner1.getSelectedItemPosition()];
roll = rand.nextInt(20) + 1;
没有使用第一行,因为您在下一行中为“roll”分配了另一个值如果您在 DICE 中保存值 6 和 20,这将是一个解决方案
int roll = rand.nextInt(DICE[spinner1.getSelectedItemPosition()])+1;
否则这应该工作
if (spinner1.getSelectedItemPosition()==0)
{
roll = rand.nextInt(6)+1; // this line was corrected after a comment
}
else
{
roll = rand.nextInt(20)+1;
}
我希望这对托马斯有所帮助
- 第二天
你好,我把它写下来并测试了它。这适用于我的 Eclipse 环境
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">StackOverflow</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="dice">
<item>6</item>
<item>10</item>
<item>20</item>
</string-array>
</resources>
Java 类
package tbn.stackoverflow;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class Starter extends Activity implements OnItemSelectedListener
{
// check twice that this array and the array in res/values/string have the same order!!
int DICE[] = {6,10,20};
Spinner spinner1;
boolean firstrun = true;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starter);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
fillSpinner();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starter, menu);
return true;
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if (firstrun)
{
firstrun = false;
return;
}
Random rand = new Random();
int roll = rand.nextInt(DICE[spinner1.getSelectedItemPosition()]) + 1;
// Put the result into a string.
String text = "You rolled a " + roll;
// a Textview is faster for testing
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(text);
// // Build a dialog box and with the result string and a single button
// AlertDialog.Builder builder = new AlertDialog.Builder(this);
// builder.setMessage(text).setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int id) {
// // do things when the user clicks ok.
// }
// });
// AlertDialog alert = builder.create();
//
// // Show the dialog box.
// alert.show();
}
private void fillSpinner()
{
// Copied from http://developer.android.com/guide/topics/ui/controls/spinner.html
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.dice, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1.setAdapter(adapter);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}