如何使用TextView显示(通过bundle传递)字符串

时间:2017-05-11 08:43:26

标签: java android

如何显示此代码中的字符串值

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

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

    if (bundle != null) {
        if (bundle.containsKey("KEY_ALL_CHECKED"))
            allChecked = bundle.getString("KEY_ALL_CHECKED");

        answer = bundle.getString("KEY_ANSWER");

在这里

textViewDisplayResult.setText();

更新后的帖子(来自MainActivity的方法checkAnswersallAnswerChecked

 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;
}

方法buttonCheckAnswer

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

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

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



            startActivity(intent);

        }
    });

AnswerActivity.java

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);


    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        if (bundle.containsKey("KEY_ALL_CHECKED"))

            textViewDisplayResult.setText(bundle.getString("KEY_ALL_CHEC‌​KED") + " "+ bundle.getString("KEY_ANSWER"));
        }

    }

}

3 个答案:

答案 0 :(得分:1)

您可以直接使用。

设置文本
textViewDisplayResult.setText(bundle.getString("KEY_ALL_CHECKED") + " "+ bundle.getString("KEY_ANSWER"));

答案 1 :(得分:0)

你是说这个吗?

String answer = bundle.getString("KEY_ANSWER");
textViewDisplayResult.setText(answer);

答案 2 :(得分:0)

您可以在文本视图中设置bunlde值,如下所示:

textViewDisplayResult.setText(answer);
textViewDisplayResult.setText(allChecked);