不确定如何使用Android在新活动中显示结果

时间:2014-08-09 23:02:40

标签: java android eclipse toast

嗨,我有一个问题,我是Android开发新手...我想在按下提交按钮后显示结果,而不是使用吐司功能,但我不知道我是怎么做的,任何帮助,将不胜感激。

这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_system);
    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) {

                Toast.makeText(SystemActivity .this, "Well done", 5).show();

            }

                else {
                    Toast.makeText(SystemActivity .this, "Fail", 5).show();




            }
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.system, menu);
    return true;
}

}

2 个答案:

答案 0 :(得分:1)

创建方法

private void displayResult(String result) {
   Intent i = new Intent(this, showResult.class);
   i.putExtra("unique_constant", result);
   startActivity(i);
}

代替吐司写,

  displayResult("Well done");

创建一个名为showResult的新Activity,确保它已添加到AndroidManifest文件中。在布局中添加textView。

在oncreate方法中写,

     String result = getIntent().getStringExtra("unique_constant"); TextView txtView = (TextView) findViewById(R.id.txtViewId); txtView.setText(result);

答案 1 :(得分:0)

您可能想要使用意图。

查看类似问题Android: passing data back and forth using intent setResults