Activity进阶

这里写图片描述
ActivityManager负责创建Activity的实例并调用其onCreate()方法

package com.atguigu.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.atguigu.geoquiz.bean.Question;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private TextView mtv_main_question;
    private Button mbtn_main_true;
    private Button mbtn_main_false;
    private Button mbtn_main_next;
    private Button mCheat_button;
    private Question[] mQuestions = new Question[]{
            new Question(R.string.question1,false),
            new Question(R.string.question2,false),
            new Question(R.string.question3,false),
            new Question(R.string.question4,false),
            new Question(R.string.question5,false)
    };
    private int mCurrentIndex = 0;
    private static final int REQUEST_CODE_CHEAT = 0;
    private boolean mIsCheater;

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

        mbtn_main_true = (Button)findViewById(R.id.btn_main_true);
//        mbtn_main_true = (Button)findViewById(R.id.tv_main_question);
        mbtn_main_false = (Button)findViewById(R.id.btn_main_false);
        mtv_main_question = (TextView)findViewById(R.id.tv_main_question);
        mbtn_main_next = (Button)findViewById(R.id.btn_main_next);
        mCheat_button = (Button)findViewById(R.id.cheat_button);

//        if(savedInstanceState != null) {
//            String question = savedInstanceState.getString("question");
//            int id = savedInstanceState.getInt("id");
//            mCurrentIndex = id;
//            mtv_main_question.setText(question);
//        }

        updateQuestion();
        initListener();
    }

    //判断答案
    private void checkAnswer(boolean userPressedTrue) {
        if(mCurrentIndex >= mQuestions.length) {
            mCurrentIndex = mQuestions.length - 1;
            return;
        }

        boolean answerTrue = mQuestions[mCurrentIndex].isAnswerTrue();
        int messageResId = 0;

        if(mIsCheater) {
            messageResId = R.string.judgment_toast;
        }else{
            if(userPressedTrue == answerTrue) {//回答正确
                messageResId = R.string.correct;
                mCurrentIndex++;
            }else{//回答错误
                messageResId = R.string.error;
                mCurrentIndex++;
            }
        }
        Toast.makeText(MainActivity.this, messageResId, Toast.LENGTH_SHORT).show();
    }

    //下一题
    private void updateQuestion() {
        mtv_main_question.setText(mQuestions[mCurrentIndex].getTextResId());
    }

    private void initListener() {
        mbtn_main_true.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this, R.string.correct, Toast.LENGTH_SHORT).show();
                checkAnswer(true);
                if(mCurrentIndex >= mQuestions.length) {
                    return;
                }
                updateQuestion();
            }
        });

        mbtn_main_false.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this, R.string.error, Toast.LENGTH_SHORT).show();
                checkAnswer(false);
                if(mCurrentIndex >= mQuestions.length) {
                    return;
                }
                updateQuestion();
            }
        });

        mbtn_main_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex++;
                mIsCheater = false;
                if(mCurrentIndex >= mQuestions.length) {
                    Toast.makeText(MainActivity.this, R.string.outofArray, Toast.LENGTH_SHORT).show();
                    mCurrentIndex = mQuestions.length - 1;
                    return;
                }
                updateQuestion();
            }
        });

        mCheat_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //start CheatActivity
                if(mCurrentIndex >= mQuestions.length) {
                    mCurrentIndex = mQuestions.length - 1;
                }
                    boolean answerTrue = mQuestions[mCurrentIndex].isAnswerTrue();
                    //如果需要多个参数,在newIntent()方法中传入多个参数
                    Intent intent = CheatActivity.newIntent(MainActivity.this, answerTrue);
                    startActivityForResult(intent,REQUEST_CODE_CHEAT);
            }
        });
    }

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

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
//        outState.putString("question",mQuestions[mCurrentIndex].getTextResId()+"");
//        outState.putInt("id",mCurrentIndex);
    }
}
package com.atguigu.geoquiz;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CheatActivity extends Activity {

    private static final String EXTRA_ANSWER_IS_TRUE = "com.atguigu.geoquiz.answer_is_true";
    private static final String EXTRA_ANSWER_SHOWN = "com.atguigu.geoquiz.answer_shown";
    private boolean mAnswerIsTrue;
    private TextView mAnswerTextView;
    private Button mShowAnswerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cheat);
        mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE,false);

        initView();

        initLinsten();
    }

    private void initView() {
        mAnswerTextView = (TextView) findViewById(R.id.answerTextView);
        mShowAnswerButton = (Button) findViewById(R.id.showAnswerButton);
    }

    private void initLinsten() {
        mShowAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mAnswerIsTrue) {
                    mAnswerTextView.setText(R.string.mtrue);
                }else{
                    mAnswerTextView.setText(R.string.mfalse);
                }
                setAnswerShownResult(true);
            }
        });
    }

    //抽取成公共的传参方法
    public static Intent newIntent(Context packageContext,boolean answerIsTrue){
        Intent intent = new Intent(packageContext, CheatActivity.class);
        intent.putExtra(EXTRA_ANSWER_IS_TRUE,answerIsTrue);
        return intent;
    }

    public static boolean wasAnswerShown(Intent result){
        return result.getBooleanExtra(EXTRA_ANSWER_SHOWN,false);
    }

    private void setAnswerShownResult(boolean isAnswerShown){
        Intent data = new Intent();
        data.putExtra(EXTRA_ANSWER_SHOWN,isAnswerShown);
        //RESULT_FIRST_USER:用户自定义结果代码
        setResult(RESULT_OK,data);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值