如果您使用 anImageSwitcher
这是一件非常简单的事情。你必须Gallery
用你的两个替换Buttons
并将它们链接到ImageSwitcher
:
private int[] mImageIds= //.. the ids of the images to use
private int mCurrentPosition = 0; // an int to monitor the current image's position
private Button mPrevious, mNext; // our two buttons
两者buttons
将有两个onClick
回调:
public void goPrevious(View v) {
mCurrentPosition -= 1;
mViewSwitcher.setImageResource(mImageIds[mCurrentPosition]);
// this is required to kep the Buttons in a valid state
// so you don't pass the image array ids boundaries
if ((mCurrentPosition - 1) < 0) {
mPrevious.setEnabled(false);
}
if (mCurrentPosition + 1 < mImageIds.length) {
mNext.setEnabled(true);
}
}
public void goNext(View v) {
mCurrentPosition += 1;
mViewSwitcher.setImageResource(mImageIds[mCurrentPosition]);
// this is required to kep the Buttons in a valid state
// so you don't pass the image array ids boundaries
if ((mCurrentPosition + 1) >= mImageIds.length) {
mNext.setEnabled(false);
}
if (mCurrentPosition - 1 >= 0) {
mPrevious.setEnabled(true);
}
}
您必须记住禁用方法Button
中的前一个onCreate
(因为我们从数组中的第一个图像开始)。