天天看点

《Android编程权威指南》第二版 第六章 挑战练习

6.4 挑战练习:报告编译版本

在GeoQuiz应用页面布局上增加一个TextView组件,向用户报告设备运行系统的API级别

首先在布局中添加TextView组件

<TextView
        android:id="@+id/api_level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
           

创建实例之后在onCreate()中添加如下方法即可

int version = Integer.valueOf(Build.VERSION.SDK_INT);
        mShowApiLevel.setText("API level "+version);
           

贴上布局和Activity的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".CheatActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/warning_text"/>
    <TextView
        android:id="@+id/answerTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        tools:text="Answer"/>
    <Button
        android:id="@+id/showAnswerButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/show_answer_button"
        />
    <TextView
        android:id="@+id/api_level"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
           
package com.example.fkxx.geoquiz;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends AppCompatActivity {

    private boolean mAnswerIsTrue;

    private TextView mAnswerTextView;

    private TextView mShowApiLevel;

    private Button mShowAnswer;

    public static boolean mWasCheat = false;

    private static final String TAGG = "CheatActivity";

    private static final String DO = "Cheat?";

    private static final String EXTRA_ANSWER_IS_TRUE ="com.example.fkxx.geoquiz.answer_is_true";

    private static final String EXTRA_ANSWER_SHOWN = "com.example.fkxx.geoquiz.answer_shown";

    public static Intent newIntent(Context packageContent,boolean answerIsTrue){
        Intent i = new Intent(packageContent,CheatActivity.class);
        i.putExtra(EXTRA_ANSWER_IS_TRUE,answerIsTrue);
        return i;
    }
    public static boolean wasAnswerShown(Intent result){
        mWasCheat = result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false);
        return result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false);

    }
    @Override
    public void onSaveInstanceState(Bundle saveInstanceState){
        super.onSaveInstanceState(saveInstanceState);
        Log.d(TAGG,"onSaveInstanceState");
        saveInstanceState.putBoolean(DO,mWasCheat);
    }
    @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.answerTextView);

        mShowAnswer = (Button)findViewById(R.id.showAnswerButton);

        mShowApiLevel = (TextView)findViewById(R.id.api_level);

        mShowAnswer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mAnswerIsTrue){
                    mAnswerTextView.setText(R.string.true_button);
                }else {
                    mAnswerTextView.setText(R.string.false_button);
                }
                mWasCheat = true;
                setAnswerShownResult(true);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
                    int cx = mShowAnswer.getWidth();
                    int cy = mShowAnswer.getHeight();
                    float radius = mShowAnswer.getWidth();
                    Animator anim = ViewAnimationUtils.createCircularReveal(mShowAnswer,cx,cy,radius,0);
                    anim.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            mShowAnswer.setVisibility(View.INVISIBLE);
                        }
                    });
                    anim.start();
                }else {
                    mShowAnswer.setVisibility(View.INVISIBLE);
                }
            }

        });if (savedInstanceState != null){
            mWasCheat = savedInstanceState.getBoolean(DO,false);
        }
        if (mWasCheat){
            if (mAnswerIsTrue){
                mAnswerTextView.setText(R.string.true_button);
            }else {
                mAnswerTextView.setText(R.string.false_button);
            }
        }
        int version = Integer.valueOf(Build.VERSION.SDK_INT);
        mShowApiLevel.setText("API level "+version);
    }
    private void setAnswerShownResult(boolean isAnswerShown){
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN,isAnswerShown);
        setResult(RESULT_OK,data);
    }
}