Android编程权威指南(第2版)第1/2章中的挑战练习思路

第一章

P46

2.8:为TextView添加监听器

//以下设置点击问题区域可以跳转到下一题
mQuestionTextView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
        updateQuestion();
    }
});

 

2.9:添加后退按钮

(一)布局文件添加prev按钮

(二)修改控制层的代码:设置点击PREV按钮跳转到上一题目,并显示题目内容

 

2.10:从按钮到图标按钮

(一)修改布局文件

android:id="@+id/prev_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow_left"
    android:contentDescription="@string/prev_button"
    />

android:id="@+id/next_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow_right"
    android:contentDescription="@string/next_button"/>

 

(二)修改QuizActivity.java


mNextButton = (ImageButton) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) {
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        //int question = mQuestionBank[mCurrentIndex].getTextResId();
        //mQuestionTextView.setText(question);
        updateQuestion();
    }
});
updateQuestion();

//以下设置点击问题区域可以跳转到下一题
mQuestionTextView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        mCurrentIndex = (mCurrentIndex +1) % mQuestionBank.length;
        updateQuestion();
    }
});

//设置点击PREV按钮跳转到上一题目,并显示题目内容
mPrevButton = (ImageButton) findViewById(R.id.prev_button);
mPrevButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        if(mCurrentIndex == 0){
            mCurrentIndex = (mQuestionBank.length - 1);
            updateQuestion();
        }else{
            mCurrentIndex = mCurrentIndex - 1;
            updateQuestion();
        }
        //updateQuestion();
    }
});

 


你可能感兴趣的:(编程成长之路)