我有2张图片可绘制。我想要做的是用一个小“下一个”按钮显示第一个图像,当点击下一个按钮时,应该用“prev”按钮显示另一个图像。现在当点击prev按钮时,第一个图像应该显示“next”按钮。这里是我的代码:如何使用next和prev按钮在两个图像之间切换?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Piqlout extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.piq);
Button next= (Button) findViewById(R.id.next);
if (next.getText().equals("Next")) {
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView img = (ImageView) findViewById(R.id.imageview);
img.setImageResource(R.drawable.piq2);
Button next= (Button) findViewById(R.id.next);
next.setText("Prev");
}
});
}
if (next.getText().equals("Prev")){
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView img = (ImageView) findViewById(R.id.imageview);
img.setImageResource(R.drawable.piq1);
Button next= (Button) findViewById(R.id.next);
next.setText("Next");
}
});
}
}
}
这里是我的布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitCenter"
android:src="@drawable/piq1" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginBottom="15dp"
android:layout_marginRight="10dp"
android:layout_gravity="bottom|right"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:background="@drawable/buttonback"
android:textColor="#000000"
android:text="Next" />
</FrameLayout>
正在发生的事情是第一个图像显示用“下一步”按钮。当我点击下一个时,第二个图像也会出现,按钮文本将变为“prev”。但是当我点击prev时什么也没有发生(第一个图像不显示)。请帮帮我。
当检查Prev时删除if语句,并将其作为else连接到第一个if语句。这样的; if(next){} else {} – Aashir
nope。现在还在发生同样的事情 –