Android编程权威指南范例---步骤一--从按钮到图标按钮

这是一个回答问题的简易app,点击true,false按钮可对问题回答正确与否进行判断,点击下一个或者上一个问题的图标可跳转到下一个问题或上一个问题。
详细代码:

实体类

public class Question {
    private int mTextResId;//问题的资源id
    private boolean mAnswerTrue;//问题的答案true/false

    public Question(int textResId, boolean answerTrue) {
        mTextResId = textResId;
        mAnswerTrue = answerTrue;
    }

    public int getTextResId() {
        return mTextResId;
    }

    public void setTextResId(int textResId) {
        mTextResId = textResId;
    }

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

    public void setAnswerTrue(boolean answerTrue) {
        mAnswerTrue = answerTrue;
    }
}

activity

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class QuizeActivity extends AppCompatActivity {

    private Button true_btn,false_btn;
    private TextView mQuestion_text;
    private ImageButton next_btn,last_btn;
    //创建question对象数组
    private Question[] mQuestions=new Question[]{
            new Question(R.string.question_one,true),
            new Question(R.string.question_two,true),
            new Question(R.string.question_three,false),
            new Question(R.string.question_four,false)
    };
    //创建数组索引
    private int mCurrentIndex=0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quize);

        true_btn=(Button)findViewById(R.id.true_button);
        false_btn=(Button)findViewById(R.id.false_button);
        mQuestion_text=(TextView)findViewById(R.id.question_next_view);
        next_btn=(ImageButton) findViewById(R.id.next_button);
        last_btn=(ImageButton)findViewById(R.id.last_button);
//        int question=mQuestions[mCurrentIndex].getTextResId();//string资源对象
//        mQuestion_text.setText(question);//设置问题的内容
        update();//设置初始问题

        true_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(true);
            }
        });
        false_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkAnswer(false);
            }
        });
        next_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex=(mCurrentIndex+1)%mQuestions.length;
                update();
            }
        });

        last_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex=(mCurrentIndex-1)%mQuestions.length;
                update();
            }
        });
    }

    private void update(){
        int question=mQuestions[mCurrentIndex].getTextResId();
        mQuestion_text.setText(question);
    }
//判断用户的答案正确与否
    private void checkAnswer(boolean userResult){
        boolean questionresult=mQuestions[mCurrentIndex].isAnswerTrue();//获取正确答案
        int textResId=0;//toast显示的内容id
//将用户答案与正确答案比较,相等则用户回答正确
        if(userResult==questionresult){
            textResId=R.string.correct_toast;
        }else{
            textResId=R.string.incorrect_toast;
        }

        Toast.makeText(this, textResId, Toast.LENGTH_SHORT).show();
    }

}

xml

<?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"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:id="@+id/question_next_view"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button"
            android:id="@+id/true_button"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button"
            android:id="@+id/false_button" />
    </LinearLayout>

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

<!--直接用inageButton,imagebutton继承自ImageView,Button继承自Button-->
       <ImageButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/arrow_left"
           android:id="@+id/last_button"/>

       <ImageButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/next_button"
           android:src="@drawable/arrow_right"/>
           //如果是button在右侧添加图片的效果
       <!--android:text="@string/next"-->
       <!--android:drawableRight="@drawable/arrow_right"/>-->

   </LinearLayout>
</LinearLayout>

效果图

这里写图片描述

猜你喜欢

转载自blog.csdn.net/ayangann915/article/details/81144948