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:从按钮到图标按钮

(一)修改布局文件

<ImageButton
    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"
    />

<ImageButton
    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();
    }
});

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值