我有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。現在還在發生同樣的事情 –