【问题标题】:My next and previous button working only once but i want to make it continue but how?我的下一个和上一个按钮只工作一次,但我想让它继续但是如何?
【发布时间】:2014-03-16 08:15:16
【问题描述】:

我在一页上有 2 张图片,我想通过使用下一个和上一个按钮(例如(1-2 张图片)然后(3-4 张图片)然后(5-6 张图片))将其更改 3 次,然后我点击在上一个按钮上,它会显示为(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++;
    }
 }

【问题讨论】:

  • 您确定要在previousClick 上使用counter++; 吗?

标签: android arrays switch-statement buttonclick


【解决方案1】:

你又问了同样的问题。无论如何,应该是

counter--;

而不是

counter++;

在previousClick(View v)的最后一行

【讨论】:

    【解决方案2】:

    首先,捕获异常并且什么都不做是一种非常糟糕的做法。至少有某种日志记录。

    实际问题似乎是由无限增加计数变量引起的。您应该重置它或使用模数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2020-08-01
      相关资源
      最近更新 更多