文章标题

来源:互联网 发布:centos 7 重置密码 编辑:程序博客网 时间:2024/06/03 22:31

第一天学习安卓开发准备开始写博客记录自己的学习过程,我现在使用的是Android Studio开发,用书是Android编程权威指南。这是书上的第一个简单的练习,主要熟悉了resource id 和xml,以及监听,用Toast方法告知答案的对错。

QuizActivity.java

package com.example.wkc.geoquiz;import android.os.Bundle;import android.support.design.widget.FloatingActionButton;import android.support.design.widget.Snackbar;import android.support.v7.app.AppCompatActivity;import android.support.v7.widget.Toolbar;import android.view.View;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class QuizActivity extends AppCompatActivity {    private Button trueButton;    private Button falseButton;    private Button nextButton;    private Button backButton;    private TextView questionView;    private int questionNumber = 0;    private TrueFalse[] question = new TrueFalse[]{            new TrueFalse(R.string.question_text1,true),            new TrueFalse(R.string.question_text2,false),            new TrueFalse(R.string.question_text3,true),            new TrueFalse(R.string.question_text4,false),            new TrueFalse(R.string.question_text5,false),    };    /**     * update the view of the question     */    public void update(){        int id = question[questionNumber].getmQuestion();        questionView.setText(id);    };    /**     * check the answer is correct or not     */    private void checkAnswer(boolean answer){        boolean realAnswer = question[questionNumber].ismTrueQuestion();        if(answer == realAnswer){            Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();        }        else {            Toast.makeText(QuizActivity.this,R.string.false_toast,Toast.LENGTH_SHORT).show();        }    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_quiz);        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);        fab.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)                        .setAction("Action", null).show();            }        });        trueButton = (Button)findViewById(R.id.tureButton);        falseButton = (Button)findViewById(R.id.falsebuton);        nextButton = (Button)findViewById(R.id.next_button);        backButton = (Button)findViewById(R.id.back_button);        questionView = (TextView)findViewById(R.id.textView);        int i = question[questionNumber].getmQuestion();        questionView.setText(i);        //set button true listener        trueButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                checkAnswer(true);            }        });        //set false button listener        falseButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                checkAnswer(false);            }        });        //set the next button listener        nextButton.setOnClickListener(new View.OnClickListener(){            public void onClick(View v){                if(questionNumber<5) {                    questionNumber = questionNumber + 1;                    update();                }                else{                    questionNumber = 0;                    update();                }            }        });        backButton.setOnClickListener(new View.OnClickListener(){            public void onClick(View v){                if(questionNumber>0) {                    questionNumber = questionNumber - 1;                    update();                }                else{                    questionNumber = 0;                    update();                }            }        });    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.menu_quiz, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        //noinspection SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

TureFalse.java

ackage com.example.wkc.geoquiz;/** * Created by WKC on 2016/2/24. * mQuestion save the ID number of the question */public class TrueFalse {    private int mQuestion;    private boolean mTrueQuestion;    public int getmQuestion() {        return mQuestion;    }    public void setmQuestion(int mQuestion) {        this.mQuestion = mQuestion;    }    public boolean ismTrueQuestion() {        return mTrueQuestion;    }    public void setmTrueQuestion(boolean mTrueQuestion) {        this.mTrueQuestion = mTrueQuestion;    }    public TrueFalse(int mQuestion, boolean mTrueQuestion){        this.mQuestion = mQuestion;        this.mTrueQuestion = mTrueQuestion;    }}
0 0
原创粉丝点击