我有10张可绘画的图片。在xml中有textview,imageview和2个用于更改图像的按钮。图像随着下一个和后退按钮的点击而变化。但它在textview中只显示一个名称,并且名称在图像更改时不会更改。 Plz给我一个解决方案。这是主要活动。
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.bnext);
gButton = (Button) findViewById(R.id.bprev);
//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]);
// I am using this to set image names in TextView
myPicText=(TextView) findViewById (R.id.idTextView);
myPicText.findViewById (R.drawable.a1);
}
};
}
请指导我在图像视图中图像更改时更改图像名称时需要做些什么。
答案 0 :(得分:0)
一种解决方案是制作并行String []:
String[] captions = { "Text for image1", "Text for image2",....};
然后在你的听众中做:
myPicText.setText(captions[currentImage]);