我是Android开发的新手,当我点击提交按钮时,我的结果将不会显示,而只显示空白的结果页面。我正在使用随机生成器,所以我希望每次按下提交按钮时结果都是随机的。我一直试图让它工作几个小时,所以任何帮助将不胜感激
这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_system);
final Random generator = new Random();
correctResults = new String[] {"good job", "well done", "good"};
RadioGroup gender = (RadioGroup) findViewById(R.id.answer1);
gender.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.answer1A:
ans1 = 1;
break;
case R.id.answer1B:
ans1 = 2;
break;
}
}
});
RadioGroup nutrition = (RadioGroup) findViewById(R.id.answer2);
nutrition.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.answer2A:
ans2 = 1;
break;
case R.id.answer2B:
ans2 = 2;
break;
}
}
});
btnSubmitQuiz = (Button) findViewById(R.id.submit);
btnSubmitQuiz.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 1
if (ans1 == 1 && ans2 == 1) {
displayResult();
getRandomCorrectResult();
} else {
displayResult();
}
}
private void displayResult() {
Intent i = new Intent("com.example.system.SHOWRESULT");
startActivity(i);
}
public String getRandomCorrectResult() {
return correctResults[generator.nextInt(correctResults.length)];
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.system, menu);
return true;
}
答案 0 :(得分:2)
似乎您可以使用此link来了解如何开始其他活动。
Intent intent = new Intent(this/context, SHOWRESULT.class);
startActivity(intent);
如果您想将值传递给其他活动,则需要使用intent.putExtra();
Intent i = new Intent(this/context, SHOWRESULT.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");
并将这些值检索到SHOWRESULT activity
,您可以使用捆绑包。
Bundle extras = getIntent().getExtras();
if (extras == null) {
//no data is passed
}
// get data via the key
String value1 = extras.getString("Value1");
if (value1 != null) {
// do something with the data
}
答案 1 :(得分:0)
有很多问题。首先,您要向您要显示的任何活动发送空Intent
...假设您想要在单独的活动中显示结果,您需要在Intent
上添加一些额外内容。
Intent i = new Intent(YourCurrentActivity.this, SHOWRESULT.class);
i.putExtra("NAME", "VALUE");
startActivity(i);
然后在你正在开始的Activity
上收到额外内容......
Bundle extras = getIntent().getExtras();
if(extras != null)
localVAriable = extras.getString("NAME");
您遇到的另一个问题是,在您启动其他Activity
displayResult();
getRandomCorrectResult();
你应该从getRandomCorrectResult
获得返回值并将它们作为额外内容放在意图中......
String results = getRandomCorrectResult();
Intent i = new Intent(YourCurrentActivity.this, SHOWRESULT.class);
i.putExtra("results", results);
startActivity(i);