使用Bundle发送一些值

时间:2017-05-09 16:06:25

标签: java android

我正在尝试传递来自MainActivity

的值
buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!allAnswersChecked())
                intent.putExtra("KEY_ALL_CHECKED", R.string.text_not_checked);

            else if (checkAnswers())
                intent.putExtra("KEY_ANSWER", R.string.Good_answer);

            else
                intent.putExtra("KEY_ANSWER", R.string.Wrong_answer);

            Bundle bundle = new Bundle();
            bundle.putString("KEY_ALL_CHECKED",getString(R.string.text_not_checked));

            startActivity(intent);
        }
});

AnswerActivity(部分我已经做过)

TextView textViewDisplayResult = (TextView) findViewById(R.id.text_view_display_result);

Bundle bundle = getIntent().getExtras();

String name = bundle.getString("KEY_ALL_CHECKED",getString(R.string.text_not_checked));

textViewDisplayResult.setText(name);

但是如何从"KEY_ANSWER"发送其他两个MainActivity值?只需在此行中添加内容即可?

String name = bundle.getString("KEY_ALL_CHECKED",getString(R.string.text_not_checked));

另一个问题

“KEY_ALL_CHECKED”在其方法

中有一个布尔值
private boolean allAnswersChecked() {
    for (boolean radioAnswer : isAnswered) {
        if (!radioAnswer) {
            return false;
        }
    }
    return true;
}

并在AnswerActivity我使用getString传递它。这可以吗?我有点困惑(代码有效)。

提前谢谢。

POST UPDATE

private static int NUMBER_OF_QUESTIONS = 3;
static boolean[] answer = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] checked = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];

PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), fragmentList);
    viewPager.setAdapter(adapter);

    final Intent intent = new Intent(MainActivity.this, AnswerActivity.class);

    buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (allAnswersChecked())
            {
                if (checkAnswers())
                    intent.putExtra("KEY_ANSWER", R.string.Good_answer);
                else
                    intent.putExtra("KEY_ANSWER", R.string.Wrong_answer);
            }
            else
                intent.putExtra("KEY_ANSWER", R.string.text_not_checked);

            startActivity(intent);
        }
    });



    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == (NUMBER_OF_QUESTIONS - 1))
                checkSelected();
            else if (buttonCheckAnswer.getVisibility() == View.VISIBLE)
                buttonCheckAnswer.setVisibility(View.GONE);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
}

public static void checkSelected() {
    for (boolean radioChecked : checked) {
        if (radioChecked) {
            buttonCheckAnswer.setVisibility(View.VISIBLE);
            break;
        }
    }
}


private boolean checkAnswers() {
    for (boolean radioAnswer : answer) {
        if (!radioAnswer) {
            return false;
        }
    }
    return true;
}

private boolean allAnswersChecked() {
    for (boolean radioAnswer : isAnswered) {
        if (!radioAnswer) {
            return false;
        }
    }
    return true;
}

AnswerActivity代码

 package make.appaplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class AnswerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answer);
        TextView textViewDisplayResult = (TextView) findViewById(R.id.text_view_display_result);


        String answer = "";
        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            if (bundle.containsKey("KEY_ANSWER"))
                answer = bundle.getString("KEY_ANSWER");
        }
        Log.d("SUCCESS", "answer: " + answer);

        textViewDisplayResult.setText(answer);
    }
}

这是一个有点改变的buttonCheckAnswer方法。它现在以正确的方式为我提供了文字"Good answer""Wrong answer"。在需要时,根本不会显示"You haven't checked all answers"

 buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (allAnswersChecked()) {
                intent.putExtra("KEY_ANSWER", "You haven't checked all answers");
            }
            if (checkAnswers())

                intent.putExtra("KEY_ANSWER", "Good Answer");

            else intent.putExtra("KEY_ANSWER", "Wrong Answer");



            startActivity(intent);
        }
    });

2 个答案:

答案 0 :(得分:1)

在您的MainActivity中,修改您的conditions,如下所示:

buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent intent = new Intent(MainActivity.this, AnswerActivity.class);

        boolean isAllAnswered = allAnswersChecked();
        Toast.makeText(getApplicationContext(), "isAllAnswered value is: " + isAllAnswered, Toast.LENGTH_SHORT).show();  

        if (isAllAnswered)
        {
            boolean isGoodAnswer = checkAnswers();
            Toast.makeText(getApplicationContext(), "isGoodAnswer value is: " + isGoodAnswer, Toast.LENGTH_SHORT).show();  

            if (isGoodAnswer)
                intent.putExtra("KEY_ANSWER", "Good Answer");
            else
                intent.putExtra("KEY_ANSWER", "Wrong Answer");
        }
        else
            intent.putExtra("KEY_ANSWER", "You haven't checked all answers");

        startActivity(intent);
    }
});

AnswerActivity中,获取values,如下所示:

    String answer = "";
    Bundle bundle = getIntent().getExtras();

    if (bundle != null) {
        if (bundle.containsKey("KEY_ANSWER"))
            answer = bundle.getString("KEY_ANSWER"); 
    }

    Log.d("SUCCESS", "answer: " + answer);

    TextView textViewDisplayResult = (TextView) findViewById(R.id.text_view_display_result);
    textViewDisplayResult.setText(answer);

希望这会有所帮助〜

答案 1 :(得分:0)

假设您要传递所有值,即使您有if..else if..else。 "如何传递其他两个KEY_ANSWER? ..."你为什么两个都使用相同的密钥?对两者使用不同的键,并将两个字符串添加到包。请尝试以下代码:

Intent intent = new Intent(this, AnswerActivity.class);
Bundle extras = new Bundle();
extras.putString("KEY_ALL_CHECKED","YOUR_STRING_VALUE");
extras.putString("KEY_ANSWER_ONE","YOUR_STRING_VALUE");
extras.putString("KEY_ANSWER_TWO","YOUR_STRING_VALUE");
intent.putExtras(extras);
startActivity(intent);

如果您想获得额外内容,请按以下方式获取三个不同的字符串:

Bundle extras = getIntent().getExtras();
String str1 = extras.getString("KEY_ALL_CHECKED");
String str2 = extras.getString("KEY_ANSWER_ONE");
String str3 = extras.getString("KEY_ANSWER_TWO");

你将获得str1,str2和str3中的所有字符串。