无法显示Intent中的String

时间:2017-05-06 14:24:16

标签: java android

在运行时我只显示一个文字Wrong_answer

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

    textViewDisplayResult.setText(getIntent().getExtras().getString("KEY_ALL_CHECKED"));
    textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false)?R.string.Good_answer:R.string.Wrong_answer);
}

POST UPDATE

public class MainActivity extends AppCompatActivity {

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

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

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

            startActivity(intent);

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

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

    textViewDisplayResult.setText(getIntent().getExtras().getString("KEY_ALL_CHECKED"));
    textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false)?getString(R.string.Good_answer):getString(R.string.Wrong_answer));

}

}

2 个答案:

答案 0 :(得分:0)

第二个setText将覆盖您的第一个setText值。

....
String x = getIntent().getExtras().getString("KEY_ALL_CHECKED");
textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false) ? x +" "+ R.string.Good_answer: x +" "+ R.string.Wrong_answer);

答案 1 :(得分:0)

textViewDisplayResult将始终覆盖。 如果您希望两者都显示在同一个textView中,您只需要附加如下文本:

textViewDisplayResult.setText(getIntent().getExtras().getString("KEY_ALL_CHECKED"));
textViewDisplayResult.append(getIntent().getBooleanExtra("KEY_ANSWER", false)?getString(R.string.Good_answer):getString(R.string.Wrong_answer));

这将在同一textView中显示您的两个结果。