具有许多活动的一个结果活动

时间:2015-05-21 15:33:54

标签: android

我有许多活动,其中包含1项结果活动。有谁知道如何分开它,结果将显示许多活动的一个结果。如果我运行它,它将显示结果fruitQuestionAnimalQuestion

Test.java:

public class Test extends Activity {
    Button btnFruitQ,btnAnimalQ;
    ToggleButton tb;

    public static boolean tbflag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        btnFruitQ = (Button) findViewById(R.id.btnFruitQ);
        btnAnimalQ = (Button) findViewById(R.id.btnAnimalQ);
        tb = (ToggleButton) findViewById(R.id.ToggleButton);
        btnFruitQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tbflag=tb.isChecked();
                startActivity(new Intent(Test.this, FruitQuestion.class));
            }
        });
        btnAnimalQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tbflag=tb.isChecked();
                startActivity(new Intent(Test.this, AnimalQuestion.class));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

FruitQuestion.java:

public class FruitQuestion extends Activity {

    TextView tv;
    Button btnNext;
    RadioGroup rg;
    RadioButton bt1,bt2,bt3;
    String Question[]={"HAHA","HAHA2","HAHA3"};
    String ans[]={"i1","12","i3"};
    String opt[]={"i1","lali","uu","12","hehe","oo","i3","jj","cc"};

    int flag=0;
    public static int mark,correct,wrong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fruit_question);

        tv=(TextView) findViewById(R.id.tvque);
        btnNext=(Button) findViewById(R.id.btnNext);
        rg = (RadioGroup) findViewById(R.id.rg);
        bt1 = (RadioButton) findViewById(R.id.btn1);
        bt2 = (RadioButton) findViewById(R.id.btn2);
        bt3 = (RadioButton) findViewById(R.id.btn3);

        tv.setText(Question[flag]);
        bt1.setText(opt[0]);
        bt2.setText(opt[1]);
        bt3.setText(opt[2]);

        Toast.makeText(this,"Negative Mark : "+Test.tbflag,1000).show();
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
                String ansText= uans.getText().toString();

                if (ansText.equalsIgnoreCase(ans[flag]))
                {
                    correct++;
                }
                else
                {
                    wrong++;
                }
                flag++;
                if(flag<Question.length)
                {
                    tv.setText(Question[flag]);
                    bt1.setText(opt[flag*3]);
                    bt2.setText(opt[(flag*3)+1]);
                    bt3.setText(opt[(flag*3)+2]);
                }
                else
                {
                    if(Test.tbflag)
                    {
                        mark=correct-wrong;
                    }
                    else
                    {
                        mark=correct;
                    }
                    Intent in=new Intent(getApplicationContext(),ResultActivity.class);
                    startActivity(in);
                }
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_fruit_question, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

ResultActivity.java:

public class ResultActivity extends Activity {
    TextView tv;
    Button btnRestart;
    StringBuffer sb=new StringBuffer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        tv=(TextView)findViewById(R.id.tvres);
        btnRestart=(Button)findViewById(R.id.btnRestart);


            sb.append("Correct Answer : " + FruitQuestion.correct);
            sb.append("\nWrong Answer : " + FruitQuestion.wrong);
            sb.append("\nFinal Score : " + FruitQuestion.mark);
            tv.setText(sb);
            FruitQuestion.correct = 0;
            FruitQuestion.wrong = 0;

        sb.append("Correct Answer : "+AnimalQuestion.correct);
        sb.append("\nWrong Answer : "+AnimalQuestion.wrong);
        sb.append("\nFinal Score : " + AnimalQuestion.mark);
        tv.setText(sb);
        AnimalQuestion.correct=0;
        AnimalQuestion.wrong=0;

        btnRestart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in=new Intent(getApplicationContext(),Test.class);
                startActivity(in);
            }
        });
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_result, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

3 个答案:

答案 0 :(得分:0)

我想我理解你,你想把一些值从A活动传递给B活动。

要在活动之间传递数据,您可以使用与发送相同的意图。 这是一个例子:

Intent i = new Intent(this, ActivityA.class);
i.putExtra("key1", value1);
i.putExtra("key2", value2);
startActivity(i);

putExtra方法允许传递多种值,如boolean,string,integer ......

然后,在另一个活动中,在onCreate方法中,您可以这样获取此值:

String save = getIntent().getExtras().getString("key1");

或者

Bundle bundle = getIntent().getExtras();
boolean check = bundle.getBoolean("key2");

我希望它可以帮到你:)。

答案 1 :(得分:0)

首先,为您的Intent实例指定一个不同的名称。除了in之外。您可以在intent对象中添加额外内容并将其发送到ResultActivity

喜欢这个

Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("activityName", "FruitQuestion");
startActivity(intent);

ResultActivity

Intent intent = getIntent();
String activityName = intent.getStringExtra("activityName");

检查您的activityName值并显示其结果。

答案 2 :(得分:0)

您可以使用Intent Extras告诉ResultActivity哪个Activity调用它,并显示相应的结果。

请注意,您还应该对代码进行重新分解,以便在“活动”中不使用静态变量来存储数据,而是可以使用SharedPreferences。

为了让你开始,这是你在短期内可以做的事情:

FruitQuestion中,您将拥有:

    Intent in=new Intent(getApplicationContext(),ResultActivity.class);
    in.putExtra("question", "fruit");
    startActivity(in);

AnimalQuestion中,你会有:

    Intent in=new Intent(getApplicationContext(),ResultActivity.class);
    in.putExtra("question", "animal");
    startActivity(in);

然后,在ResultActivity中,捕获Intent Extra,并显示相应的结果:

    Bundle extras = this.getIntent().getExtras();
    if (extras != null){
        String question = extras.getString("question");
        if (question.equals("fruit")) {
            sb.append("Correct Answer : " + FruitQuestion.correct);
            sb.append("\nWrong Answer : " + FruitQuestion.wrong);
            sb.append("\nFinal Score : " + FruitQuestion.mark);
            tv.setText(sb);
            FruitQuestion.correct = 0;
            FruitQuestion.wrong = 0;
        }
        else if (question.equals("animal")) {
            sb.append("Correct Answer : " + AnimalQuestion.correct);
            sb.append("\nWrong Answer : " + AnimalQuestion.wrong);
            sb.append("\nFinal Score : " + AnimalQuestion.mark);
            tv.setText(sb);
            AnimalQuestion.correct = 0;
            AnimalQuestion.wrong = 0;
        }
    }