从活动1的textview通过activity2的textview传递数据

时间:2013-02-06 14:30:47

标签: android

如何将textview的值传递给一个活动到另一个活动?

我的游戏得分在textview上显示,在增加之后它将意图进行下一个活动。但第一项活动的得分值未显示在第二项活动的文本视图中。

这是我第一次活动的代码

final TextView score2 = (TextView) findViewById(R.id.tvscore2);
    Button page1 = (Button) findViewById(R.id.btnDog);
    page1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            EditText etDog1 = (EditText) findViewById(R.id.etDog);
            String Dog = etDog1.getText().toString();

            if (Dog.equalsIgnoreCase("dog")) {
                global.score += 10;


                score2.setText(String.valueOf(global.score));
                Toast.makeText(getApplicationContext(), "Correct",
                        Toast.LENGTH_SHORT).show();
                Intent myIntent = new Intent(view.getContext(),
                        sound1_3pig.class);

                startActivityForResult(myIntent, 0);

            } else if (global.score <= 0) {
                global.score += 0;
                score2.setText(String.valueOf(global.score));
                Toast.makeText(getApplicationContext(), "Wrong",
                        Toast.LENGTH_SHORT).show();
            } else {

                global.score -= 5;
                score2.setText(String.valueOf(global.score));
                Toast.makeText(getApplicationContext(), "Wrong",
                        Toast.LENGTH_SHORT).show();
            }
        }

    });

}

我想将得分活动1的结果显示在第二个活动的文本视图中

这是我的第二项活动

final TextView score1 = (TextView) findViewById(R.id.tvscore1);
    Button page1 = (Button) findViewById(R.id.btnCat);
    page1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            EditText etCat = (EditText) findViewById(R.id.etCat);
            String Cat = etCat.getText().toString();
            if (Cat.equalsIgnoreCase("cat")) {

                global.score += 10;
                score2.setText(String.valueOf(global.score));

                Toast.makeText(getApplicationContext(), "Correct",
                        Toast.LENGTH_SHORT).show();

                Intent myIntent = new Intent(view.getContext(),
                        sound1_3pig.class);
                startActivityForResult(myIntent, 0);
                finish();

            } else if (global.score <= 0) {
                global.score += 0;
                score2.setText(String.valueOf(global.score));
                Toast.makeText(getApplicationContext(), "Wrong",
                        Toast.LENGTH_SHORT).show();

            } else {
                global.score -= 5;
                score2.setText(String.valueOf(global.score));
                Toast.makeText(getApplicationContext(), "Wrong",
                        Toast.LENGTH_SHORT).show();
            }

        }
    });

}

3 个答案:

答案 0 :(得分:1)

使用类似的东西。

活动1:

Intent i = new Intent(getActivity(), Activity2.class);
i.putExtra("Score", 20);

startActivity(i);

活动2:

Intent i = getIntent();
int score = i.getIntExtra("Score", 0);

答案 1 :(得分:1)

当您的第二个活动膨胀新布局时,您需要显式地将值从第一个活动传递到第二个活动,并使用此值初始化它的TextView。

活动1:

void goToSecondActivity() {
    String value = mTextView.getText();
    Intent in = new Intent(getActivity(), YourSecondClass.class);
    in.putExtra("score", value);
    startActivity(in);
}

活动2:

void onCreate(Bundle bundle) {
    ...
    String score = getIntent().getStringExtra("score", "No score.");
    mTextView.setText(score);
}

答案 2 :(得分:0)

我的答案也与其他人的建议类似。试试这个,我在你的代码中添加了一些额外的语句(我假设你想要将变量score的值发送到下一个活动,你可以用你要发送的变量替换它):

第一项活动:

score2.setText(String.valueOf(score));
Toast.makeText(getApplicationContext(), "Correct",
             Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(this,
                    sound1_3pig.class);
myIntent.putExtra("Score", global.score);
startActivityForResult(myIntent, 0);

第二项活动:

将它放在第二个活动的onCreate()方法中:

Intent intent = getIntent();
int score = intent.getIntExtra("Score",0);

所以现在您将获得第二个活动中上一个活动的得分值。然后你可以通过调用

在想要显示它的textView中设置它
textView.setText(score);
相关问题