在本书的2-11中,
mFalseButton=(Button)findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast=Toast.makeText(MainActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT);
//如果这里直接写this 会指向 View.OnClickListener()
toast.setGravity(Gravity.TOP,0,0);
toast.show();
}
});
Toast toast=Toast.makeText(MainActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT);
//如果这里直接写this 会指向 View.OnClickListener()
private void checkAnswer(boolean userPressedTrue){
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnserTrue();
int messageResId=0;
if (userPressedTrue == answerIsTrue){
messageResId = R.string.correct_toast;
}
else{
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();
//因为这里的 checkAnswer是 MainActivity.class的私有方法 所以这里的this,是指MainActivity。
}
Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();
//因为这里的 checkAnswer是 MainActivity.class的私有方法 所以这里的this,是指MainActivity。
mQuestionTextView = (TextView) findViewById(R.id.question_text_view);
updateQuestion();
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex+1)%mQuestionBank.length;
//这个操作没什么难度,主要是复习本章的学习内容。
updateQuestion();
}
});
1.XML文件编写布局
编写一个纵向的LinearLayout线性布局,然后加入Pre_Button,然后加入图片资源。
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/pre_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pre_button"
android:drawablePadding="4dp"
android:drawableLeft="@drawable/arrow_left"
/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"
android:drawableRight="@drawable/arrow_right"
android:drawablePadding="4dp"
/>
</LinearLayout>
2.于controller层 MainActivity中编码
(1)添加变量 Button mPreButton
public class MainActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mPreButton;
private TextView mQuestionTextView;
(2)编写监听器
mPreButton =(Button)findViewById(R.id.pre_button);
mPreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentIndex-1<0){
mCurrentIndex = mQuestionBank.length-1;
}
else {
mCurrentIndex = (mCurrentIndex -1)% mQuestionBank.length;
}
updateQuestion();
}
});
}
if (mCurrentIndex-1<0){
mCurrentIndex = mQuestionBank.length-1;
}
如果小于零,则Index指向数组的最后一个。
(1) xml布局编码
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/pre_imgbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_left"/>
<ImageButton
android:id="@+id/next_imgbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow_right"/>
</LinearLayout>
(2)控制层编码
声明变量
private ImageButton mPreImgButton;
private ImageButton mNextImgButton;
编写监听器
mPreImgButton=(ImageButton)findViewById(R.id.pre_imgbutton);
mPreImgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mCurrentIndex-1<0){
mCurrentIndex = mQuestionBank.length-1;
}
else{
mCurrentIndex = (mCurrentIndex-1)%mQuestionBank.length;
}
updateQuestion();
}
});
mNextImgButton=(ImageButton)findViewById(R.id.next_imgbutton);
mNextImgButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex+1)%mQuestionBank.length;
updateQuestion();
}
});
主要还是熟练操作,与前面的逻辑没有太大变化。