getBooleanExtra()如何正常工作?

时间:2015-08-26 08:55:16

标签: android

我正在尝试了解The Big Nerd Ranch Android指南中的一些代码。这是代码:

public class CheatActivity extends AppCompatActivity {

private static final String EXTRA_ANSWER_IS_TRUE = "com.james.listview.geoquiz.answer_is_true";
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;

public static Intent newIntent(Context packageContext, boolean answerIsTrue) {
    Intent intent = new Intent(packageContext, CheatActivity.class);
    intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    return intent;
}

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

    mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

    mAnswerTextView = (TextView) findViewById(R.id.answer_text_view);

    mShowAnswer = (Button) findViewById(R.id.answer_button);
    mShowAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mAnswerIsTrue) {
                mAnswerTextView.setText(R.string.button_true);
            } else {
                mAnswerTextView.setText(R.string.button_false);
            }
        }
    });
}

我理解意图和传递价值观。我遇到困难的地方是on mShowAnswer。根据情况,通过getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false)收到的值可以是True或False。我的问题是:

if语句的条件如何工作?假设mAnswerIsTrue = TRUE,那么将mAnswerTextView设置为“TRUE”是有意义的。但是如果mAnswerIsTrue = FALSE,那么在我看来mAnswerTextView - 根据代码 - 应该仍然将自己设置为“TRUE”。 mAnswerTextView如何知道将自己设置为“FALSE”?

要清楚,代码按预期工作。我只是不确定它是如何运作的。

1 个答案:

答案 0 :(得分:1)

我尝试评论您的代码,以便您更好地理解:)

public class CheatActivity extends AppCompatActivity {

private static final String EXTRA_ANSWER_IS_TRUE = "com.james.listview.geoquiz.answer_is_true";
private boolean mAnswerIsTrue;
private TextView mAnswerTextView;
private Button mShowAnswer;

public static Intent newIntent(Context packageContext, boolean answerIsTrue) {
    Intent intent = new Intent(packageContext, CheatActivity.class);
    intent.putExtra(EXTRA_ANSWER_IS_TRUE, answerIsTrue);
    return intent;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cheat);
    //Here you get the boolean: you use the static key EXTRA_ANSWER_IS_TRUE and mAnswerIsTrue gain the bool value passed.
    mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

    //here you initialize the TextView so you can access it from the code
    mAnswerTextView = (TextView) findViewById(R.id.answer_text_view);
    //and here you initialize the button for the same reason
    mShowAnswer = (Button) findViewById(R.id.answer_button);
    mShowAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //now, here is the problem: this is the contract form of if statement:
            //saying if(bool){dosomething}
            //is the same as saying if(bool == true){dosomething}

            //so this code is equals to:
            if(mAnswerIsTrue == true){
                mAnswerTextView.setText(R.string.button_true);
            } else{
                //here you go if the bool is false
                mAnswerTextView.setText(R.string.button_false);
            }


            if (mAnswerIsTrue) {//this means so if(bool == true)
                mAnswerTextView.setText(R.string.button_true);
            } else { //and here you go if it is false
                mAnswerTextView.setText(R.string.button_false);
            }
        }
    });
}

我希望你明白了,如果你有问题告诉我:)