The Definitive Guide to Android Programming (Third Edition) Chapter 6 Challenge Exercises

Write in front:


Everyone is welcome to give me any comments and suggestions
. Please note that each program does not undertake the previous challenge exercises.
If you have any questions or comments, you can comment below.
Thank you!


Download Area
Chapter 6 Challenge Practice -> Link
There are other ways to provide downloads for small partners without points



Question 1: Challenge Exercise 6.4: Report Compiled Versions

Add a TextView component to the page layout of the GeoQuiz application to report to the user the API level of the device's operating system

step 1: Add TextView under ShowAnswer (activity_cheat.xml)

    <TextView
        android:id="@+id/show_api_level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="14dp"
        tools:text=""/>

Explanation: Needless to say, just add a text box to display the API Level

step 2: Add binding and set API display (CheatActivity.java)

private TextView mApiLevelTV;//别忘了创建
//...
protected void onCreate(Bundle savedInstanceState) {

//...

setContentView(R.layout.activity_cheat);

/*在上面这句后,加入绑定语句,然后设置与显示字符串*/
mApiLevelTV = (TextView)findViewById(R.id.show_api_level);
CharSequence cs = "API Level " + Build.VERSION.SDK_INT;
mApiLevelTV.setText(cs);

//...
}

Explanation: Done, here is mainly to understand what charSequance is. String also inherits from CharSequence and implements related methods, read-only.

result

Below the button of ShowAnswer shows the version of the API
Screenshot of Challenge Exercise 6.4

Question 2: Challenge Exercise 6.5: Limit the number of cheats

Allows users to cheat up to 3 times. The number of times the user peeked at the answer is recorded, and the remaining times are displayed under the CHEAT button. Once exceeded, disable the peek button.

Simple Analysis

According to my understanding of this topic, clicking the show answerbutton is considered a peek, so this is a bit troublesome, and data needs to be transmitted from both sides, from QuizActivityto to CheatActivityeach other.
Idea : QuizActiviityCreate a variable that records the number of times the answer is displayed –> pass it to CheatActivty, when the user clicks to display the answer, the variable will be decremented by one, –> return the original, QuizActivityand finally update the UI, if the number is 0, it means you can’t read it .

step 1: Add amount (QuizActivity.java and CheatActivity.java)

  • Add two quantities in the data creation section inQuizActivity.java front of andCheatActivity.java
private static final String EXTRA_CHEAT_CHANCE =
        "com.bignerdranch.android.geoquiz.cheat_chance";
//记录作弊次数
private static int mCheatChance = 3

step 2: Add TextView(activity_quiz.xml)

  • Include where appropriate TextViewto display the number of peeks remaining. I put it nextButtonbehind
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/show_cheat_chance"
        android:text=""
        />

step 3: Binding (QuizActivity.java)

  • Needless to say about control binding
mCheatChance = getIntent().getIntExtra(EXTRA_CHEAT_CHANCE,3);

step 4: incoming data (QuizActivity.java)

  • Send the current data to CheatActivity.javaChina, let him know the current number of views
 mCheatButton.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
     boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
     Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
     //下方多传入剩余可查看答案的次数
     intent.putExtra(EXTRA_CHEAT_CHANCE,mCheatChance);
     startActivityForResult(intent,REQUEST_CODE_CHEAT);
 }
});

Step 5: Get the data, click to display the answer and the data changes (CheatAcitivity.java)

  • I have onCreateposted all the code here, but in fact, just add mCheatChancetwo sentences related to
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);
        //下面这句从QuizActivity中获得可查看答案次数的数据
        mCheatChance = getIntent().getIntExtra(EXTRA_CHEAT_CHANCE,3);
        mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);

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

        mApiLevelTV = (TextView)findViewById(R.id.show_api_level);
        CharSequence cs = "API Level " + Build.VERSION.SDK_INT;
        mApiLevelTV.setText(cs);
        mShowAnswerButton = (Button)findViewById(R.id.show_answer_button);
        mShowAnswerButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            //每次看过答案就-1
            mCheatChance--;
            if(mAnswerIsTrue){
                mAnswerTextView.setText(R.string.true_button);
            }else{
                mAnswerTextView.setText(R.string.false_button);
            }
            setAnswerShownResult(true);
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                int cx = mShowAnswerButton.getWidth() / 2;
                int cy = mShowAnswerButton.getHeight() / 2;
                float radius = mShowAnswerButton.getWidth();
                Animator anim = ViewAnimationUtils
                        .createCircularReveal(mShowAnswerButton, cx, cy, radius, 0);
                anim.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        super.onAnimationEnd(animation);
                        mShowAnswerButton.setVisibility(View.INVISIBLE);
                    }
                });
                anim.start();
            }else{
                mShowAnswerButton.setVisibility(View.INVISIBLE);
            }
        }
    });
}

step 6: data is returned to QuizActivity (CheatActivity.java)

  • Use setAnswerShownResult to send it mCheatChanceback
    private void setAnswerShownResult(boolean isAnswerShown) {
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
        //顺便传一个数回去
        data.putExtra(EXTRA_CHEAT_CHANCE, mCheatChance);
        setResult(RESULT_OK, data);
    }

step 7: QuizActivity receives and updates (QuizActivity.java)

After onActivityResultreceiving the returned data

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CODE_CHEAT) {
            if (data == null) {
                return;
            }
            mIsCheater = CheatActivity.wasAnswerShown(data);
            //得到返回数据
            mCheatChance = data.getIntExtra(EXTRA_CHEAT_CHANCE,0);
        }
    }

Update the display UI in onStart

 @Override
    public void onStart() {
        super.onStart();
        //没有作弊的机会了
        if(mCheatChance == 0) {
            mCheatButton.setEnabled(false);
            mCheatChanceTextView.setText("no chance" + " times left");
        }
        else{
            mCheatChanceTextView.setText(mCheatChance + " time(s) left");
        }
        Log.d(TAG, "onStart() called");
    }

result

Screenshot of Challenge Exercise 6.5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325324631&siteId=291194637