这有点难以解释。基本上这有点像一个简单的游戏。我希望人们输入他们的名字(目前提交按钮无法正常工作),为每个名字点击“提交”,当所有名字都输入时,他们点击“播放”。然后就开始下一个类。它需要从先前的类中获取字符串数组以及玩家的数量。然后,它需要按顺序选择每个人的名字,并给他们一个要做的任务(它随机生成)。然后,其他人可以点击一个按钮,对他们的表现进行评分。 (我不确定如何设置评分系统。不确定是否有办法将分数分配给特定的数组字符串)然后我希望在 5 轮后显示获胜者。如果您有任何意见或可以帮助我,我将非常感激。感谢您抽出时间...这是我的两门类(class)。
1 级
public class Class1 extends Activity
{
int players=0;
String names[];
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.class1);
final EditText input = (EditText) findViewById(R.id.nameinput);
Button submitButton = (Button) findViewById(R.id.submit_btn);
submitButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View submit1)
{
players++;
for(int i=0; i < players; i++)
{
names[i] = input.getText().toString();
input.setText("");
}
}
});
Button doneButton = (Button) findViewById(R.id.done_btn);
doneButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View done1)
{
Intent done = new Intent(Class1.this, Class2.class);
done.putExtra("players", players);
done.putExtra("names", names[players]);
startActivity(done);
}
});
}
2级
public class Class2 extends Activity
{
int players, counter, score, ptasks,rindex;
String[] names, tasks;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.class2);
Intent game = getIntent();
players = game.getIntExtra("players", 1);
names = game.getStringArrayExtra("names");
Random generator = new Random();
tasks[0]= "task1";
tasks[1]= "task2";
tasks[2]= "task3";
tasks[3]= "task4";
tasks[4]= "task5";
tasks[5]= "task6";
tasks[6]= "task7";
tasks[7]= "task8";
tasks[8]= "task9";
tasks[9]= "task10";
while (counter <5)
{
for (int i = 0; i < players; i++)
{
TextView name1 = (TextView) findViewById(R.id.pname);
name1.setText( names[i]+":");
ptasks = 10;
rindex = generator.nextInt(ptasks);
TextView task = (TextView) findViewById(R.id.task);
task.setText( tasks[rindex]);
}
Button failButton = (Button) findViewById(R.id.fail_btn);
failButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View failed)
{
//not sure what to put here to get the scores set up
}
});
Button notButton = (Button) findViewById(R.id.notbad_btn);
notButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View notbad)
{
//not sure what to put here to get the scores set up
}
});
Button champButton = (Button) findViewById(R.id.champ_btn);
champButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View champp)
{
//not sure what to put here
}
});
counter++;
}
}
我确信这件事充满了错误。如果是的话,我很抱歉我不是一个经验丰富的程序员。再次感谢
最佳答案
您可以使用 Bundle 将字符串数组从一个 Activity 传递到另一个 Activity 。
Bundle bundle = new Bundle();
bundle.putStringArray("arrayKey", stringArray);
然后您可以从下一个 Activity 访问此 stringArray,如下所示:
Bundle bundle = this.getIntent().getExtras();
String[] stringArray = bundle.getStringArray("arrayKey");
我不确定这是否是您唯一打算做的事情。我希望它有帮助。另外,要将分数分配给特定的字符串数组,假设您的分数是整数,您可以使用 HashMap,如下所示,
HashMap<String[],int> imageData = new HashMap<String[],int>();
但如果您打算这样做,我不确定如何将此 map 传递给另一个 Activity 。
关于java - 如何让这个字符串数组传递到下一个类并使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768726/