我在一个页面上有2个图像,我想用下一个和上一个按钮改变它3次(1-2个图像)然后(3-4个图像)然后(5-6个图像)然后我点击在上一个按钮然后它显示(3-4图像)然后(1-2图像)。当我完成循环然后单击下一个或上一个按钮时,我的代码只工作一次然后没有任何反应,如果你在我的代码中发现任何错误然后纠正我,这是我的java代码:
public class AlifPics extends Activity {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
TextView text7;
ImageView img1;
ImageButton imgBut1;
ImageButton imgBut2;
ImageButton nextBtn;
ImageButton preBtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alif_pics);
text1 = (TextView) findViewById(R.id.textView1);
text2 = (TextView) findViewById(R.id.textView2);
text3 = (TextView) findViewById(R.id.textView3);
text4 = (TextView) findViewById(R.id.textView4);
text5 = (TextView) findViewById(R.id.textView5);
text6 = (TextView) findViewById(R.id.textView6);
text7 = (TextView) findViewById(R.id.textView7);
img1 = (ImageView) findViewById(R.id.imageView1);
imgBut1 = (ImageButton) findViewById(R.id.imageButton1);
imgBut2 = (ImageButton) findViewById(R.id.imageButton2);
nextBtn = (ImageButton) findViewById(R.id.next);
preBtn = (ImageButton) findViewById(R.id.previous);
}
// this Menu Button method goes to MainActivity
public void indexHuroof(View v) {
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
}
int counter = 1;
String[] textvalue1 = { "a2", "a3" };
String[] textvalue2 = { "b2", "b3" };
String[] textvalue3 = { "c2", "c3" };
String[] textvalue4 = { "d2", "d3" };
String[] textvalue5 = { "e2", "e3" };
String[] textvalue6 = { "f2", "f3" };
String[] textvalue7 = { "g2", "g3" };
int[] myImageViewList1 = new int[] { R.drawable.img2, R.drawable.img3 }; // 123
int[] myImageButtonList1 = new int[] { R.drawable.ibut2, R.drawable.ibut3 }; // 1,2,3
int[] myImageButtonList2 = new int[] { R.drawable.ibut5, R.drawable.ibut6 }; // 4,5,6
// my next ImageButton
public void nextClick(View v) {
try {
switch (counter) {
case 1: // first click
text1.setText(textvalue1[0]);
text2.setText(textvalue2[0]);
text3.setText(textvalue3[0]);
text4.setText(textvalue4[0]);
text5.setText(textvalue5[0]);
text6.setText(textvalue6[0]);
text7.setText(textvalue7[0]);
img1.setImageResource(myImageViewList1[0]);
imgBut1.setImageResource(myImageButtonList1[0]);
imgBut2.setImageResource(myImageButtonList2[0]);
break;
case 2: // second click
text1.setText(textvalue1[1]);
text2.setText(textvalue2[1]);
text3.setText(textvalue3[1]);
text4.setText(textvalue4[1]);
text5.setText(textvalue5[1]);
text6.setText(textvalue6[1]);
text7.setText(textvalue7[1]);
img1.setImageResource(myImageViewList1[1]);
imgBut1.setImageResource(myImageButtonList1[1]);
imgBut2.setImageResource(myImageButtonList2[1]);
break;
case 3: // third click
text1.setText(textvalue1[2]);
text2.setText(textvalue2[2]);
text3.setText(textvalue3[2]);
text4.setText(textvalue4[2]);
text5.setText(textvalue5[2]);
text6.setText(textvalue6[2]);
text7.setText(textvalue7[2]);
img1.setImageResource(myImageViewList1[2]);
imgBut1.setImageResource(myImageButtonList1[2]);
imgBut2.setImageResource(myImageButtonList2[2]);
break;
default:
break;
}
} catch (Exception e) {
}
counter++;
}
// my previous ImageButton
public void previousClick(View v) {
try
{
switch (counter) {
case 4:
text1.setText(textvalue1[1]);
text2.setText(textvalue2[1]);
text3.setText(textvalue3[1]);
text4.setText(textvalue4[1]);
text5.setText(textvalue5[1]);
text6.setText(textvalue6[1]);
text7.setText(textvalue7[1]);
img1.setImageResource(myImageViewList1[1]);
imgBut1.setImageResource(myImageButtonList1[1]);
imgBut2.setImageResource(myImageButtonList2[1]);
break;
case 3:
text1.setText(textvalue1[0]);
text2.setText(textvalue2[0]);
text3.setText(textvalue3[0]);
text4.setText(textvalue4[0]);
text5.setText(textvalue5[0]);
text6.setText(textvalue6[0]);
text7.setText(textvalue7[0]);
img1.setImageResource(myImageViewList1[0]);
imgBut1.setImageResource(myImageButtonList1[0]);
imgBut2.setImageResource(myImageButtonList2[0]);
break;
case 2:
text1.setText(R.string.textview1_default);
text2.setText(R.string.textview1_default);
text3.setText(R.string.textview1_default);
text4.setText(R.string.textview1_default);
text5.setText(R.string.textview1_default);
text6.setText(R.string.textview1_default);
text7.setText(R.string.textview1_default);
img1.setImageResource(R.drawable.img1);
imgBut1.setImageResource(R.drawable.ibut1);
imgBut2.setImageResource(R.drawable.ibut2);
break;
default:
break;
}
}
catch (Exception e) {
}
counter++;
}
}
答案 0 :(得分:0)
首先,捕捉异常并无所事事是一种非常糟糕的做法。至少有某种记录。
实际问题似乎是由无限增加计数变量引起的。你应该重置它或使用modulo。
答案 1 :(得分:0)
你再次问同样的问题。不管怎样,它应该是
counter--;
而不是
counter++;
在previousClick的最后一行(View v)