我在此感谢所有帮助我的人。
我正在尝试制作一个包含 10 张图片的应用程序,主布局上只有一个图像视图和一个按钮。
当我按下按钮时,图像变为下一个。当我再次按下按钮时,它会变为另一个。
我有几天都做不到。我正在尝试这个;
主.java;
package com.example.denemeemre;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity {
private ImageView hImageViewSemafor;
public Button hButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
hButton = (Button) findViewById(R.id.idBtnChangeImage);
hButton.setOnClickListener(aButtonChangeImageListener);
}
View.OnClickListener aButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
// setImageResource will change image in ImageView
hImageViewSemafor.setImageResource(R.drawable.r1);
}
};
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
hButton = (Button) findViewById(R.id.idBtnChangeImage);
hButton.setOnClickListener(bButtonChangeImageListener);
}
View.OnClickListener bButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
// setImageResource will change image in ImageView
hImageViewSemafor.setImageResource(R.drawable.r2);
}
};
View.OnClickListener cButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
// setImageResource will change image in ImageView
hImageViewSemafor.setImageResource(R.drawable.r3);
}
};
View.OnClickListener dButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
// setImageResource will change image in ImageView
hImageViewSemafor.setImageResource(R.drawable.r4);
}
};
View.OnClickListener eButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
// setImageResource will change image in ImageView
hImageViewSemafor.setImageResource(R.drawable.r5);
}
};
}
和main.xml;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/idImageViewSemafor"
android:layout_width="match_parent"
android:layout_height="342dp"
android:layout_weight="1.04"
android:adjustViewBounds="true"
android:background="#66FFFFFF"
android:maxHeight="91dip"
android:maxWidth="47dip"
android:padding="10dip"
android:src="@drawable/r0" />
<Button
android:id="@+id/idBtnChangeImage"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sonraki" />
</LinearLayout>
更新;
现在是这样,从第一张照片倒退;
编码;
package com.galerionsekiz;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Main extends Activity {
private ImageView hImageViewPic;
private Button iButton, gButton;
private int currentImage = 0;
int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
iButton = (Button) findViewById(R.id.bIleri);
gButton = (Button) findViewById(R.id.bGeri);
//Just set one Click listener for the image
iButton.setOnClickListener(iButtonChangeImageListener);
gButton.setOnClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage--;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
}
主要的;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill"
android:orientation="vertical"
android:weightSum="100" >
<ImageView
android:id="@+id/idImageViewPic"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="100"
android:adjustViewBounds="true"
android:background="#66FFFFFF"
android:maxHeight="91dip"
android:maxWidth="47dip"
android:padding="10dip"
android:src="@drawable/r0" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/bGeri"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Önceki" >
</Button>
<Button
android:id="@+id/bIleri"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sonraki" >
</Button>
</LinearLayout>
</LinearLayout>
有什么建议么?