2013-01-17 217 views
1

首先,我使用Andorid和Java只有一個星期,我是一個總新手。Android:如何在多個圖像按鈕上設置監聽器?

我需要知道用戶點擊了哪個按鈕,然後將其與好的或不好的答案進行比較。

下面是代碼

protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.questions); 



     Integer[] flags = { 
      R.drawable.flag_albania, 
      R.drawable.flag_andorra, 
      R.drawable.flag_angola, 
      R.drawable.flag_avganistan, 

     }; 

     String[] questions = { 
      "What is the flag of Albania", 
      "What is the flag of Andorra", 
      "What is the flag of Angola", 
      "What is the flag of Avganistan", 

     }; 



     TextView tv = (TextView) findViewById(R.id.textView1); 
     int arraySize = flags.length; 
     Random rand = new Random(); 

     ImageButton button1 = (ImageButton) findViewById(R.id.imageButton1); 
     int index = rand.nextInt(arraySize); 
     int questionIndex1 = index; 
     button1.setImageResource(flags[index]); 
     flags[index] = flags[--arraySize]; 

     ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2); 
     index = rand.nextInt(arraySize); 
     int questionIndex2 = index; 
     button2.setImageResource(flags[index]); 
     flags[index] = flags[--arraySize]; 

     ImageButton button3 = (ImageButton) findViewById(R.id.imageButton3); 
     index = rand.nextInt(arraySize); 
     int questionIndex3 = index; 
     button3.setImageResource(flags[index]); 
     flags[index] = flags[--arraySize]; 

     ImageButton button4 = (ImageButton) findViewById(R.id.imageButton4); 
     index = rand.nextInt(arraySize); 
     int questionIndex4 = index; 
     button4.setImageResource(flags[index]); 
     flags[index] = flags[--arraySize]; 

     Integer[] question = { 
       questionIndex1, 
       questionIndex2, 
       questionIndex3, 
       questionIndex4 
     }; 

     int questionArraySize = question.length; 

     int questionArray = rand.nextInt(questionArraySize); 

     tv.setText(questions[question[questionArray]]); 

我的想法是比較questionIndex是隨機選擇的按鈕標識,但我真的不知道如何實現它。每一個幫助表示讚賞。

+0

如何在switch語句中任何情況下比較這?如果(questionArray == questionIndex1) –

回答

1

使用此代碼減少

ImageButton button1,button2,button3,button4; 
      ImageButton imagebuttons[]={ button1,button2,button3,button4}; 
     int ids[]={R.id.imageButton1,R.id.imageButton2,R.id.imageButton3,R.id.imageButton4}; 

     for(final int i=0;i<imagebuttons.length;i++) 
     { 
      imagebuttons[i]=(ImageButton) findViewById(ids[i]); 
    int index=rand.nextInt(arraySize); 
      imagebuttons[i].setImageResource(flags[index]); 
      flags[index] = flags[--arraySize]; 
      indexes.put(i,index); 
      imagebuttons[i].setOnClickListener(new View.OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        if(questionArray==indexes.get(i)) 
        { 

        } 

       } 
      }); 

我希望這將有助於你

+1

謝謝!這有助於很多你能解釋我如何一起實現這個條件(questionArray == questionIndex1)。我需要得到兩個條件:按下哪個按鈕,並且該按鈕是正確的答案。謝謝! –

+0

請檢查編輯的答案 – Pragnani

+0

是你需要的,是這個代碼爲你工作...請回到我 – Pragnani

0

你可以這樣來做: 首先,讓您的活動實現的onClick監聽器:

public class MyActivity extends Activity implements View.OnClickListener{ 

然後設置操作偵聽到您的按鈕:

button1.setOnClickListener(this); 
button2.setOnClickListener(this); 
..... 

然後在監聽,支票編號:

public void onClick(View v) { 
    switch(v.getId()){ 
    case R.id.imageButton1: 
    break; 
    case R.id.imageButton2: 
    break; 
    .... 
    } 
} 
1

試試這種方式:

button1.setOnClickListener(onClickListener); 
    button2.setOnClickListener(onClickListener); 
    /** 
    * Common click listener 
    */ 
OnClickListener onClickListener = new OnClickListener() 
    { 
    @Override 
    public void onClick(View p_v) 
    { 
     switch (p_v.getId()) 
      { 
       case R.id.imageButton1: 
        //Your logic here. 
        break; 
       case R.id.imageButton2: 
        //Your logic here. 
        break; 
      } 
    } 

}

+0

好的。這很好,但如何比較這種情況?如果(questionArray == questionIndex1) –

1

下面寫代碼

public class MainActivity extends Activity implements OnClickListener{ 
    protected void onCreate(Bundle savedInstanceState) { 

     /// Your Above Code /// 

     tv.setText(questions[question[questionArray]]); 

     Imagebutton1.setOnClickListener(this); 
     Imagebutton2.setOnClickListener(this); 
     Imagebutton3.setOnClickListener(this); 
     Imagebutton4.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     if(v == Imagebutton1){ 
      // Write Your Code Here 
     } else if(v == Imagebutton2){ 
      // Write Your Code Here 
     } else if(v == Imagebutton3){ 
      // Write Your Code Here 
     } else if(v == Imagebutton4){ 
      // Write Your Code Here 
     } 
    } 
} 
0

你可以做這樣說也。你必須使用一些int變量來保持按鈕的標籤值。而且你還必須爲按鈕動作創建一個onClickListner。編碼是遵循

public class MyActivity extends Activity{ 

    //Define the Tag values for image buttons. 
    private static final int TAG_IMAGE_BTN_1 = 0; 
    private static final int TAG_IMAGE_BTN_2 = 1; 
    private static final int TAG_IMAGE_BTN_3 = 2; 
    private static final int TAG_IMAGE_BTN_4 = 3; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     initActivity(); 

    } 

    private void initActivity(){ 
      //Initialize the image buttons via xml or from the code itself. 
      //Add the onClickListner_BTN_CLICK and the corresponding tag to the image button 

      imageBtn1.setTag(TAG_IMAGE_BTN_1); 
      imageBtn1.setOnClickListener(onClickListner_BTN_CLICK); 

      imageBtn2.setTag(TAG_IMAGE_BTN_2); 
      imageBtn2.setOnClickListener(onClickListner_BTN_CLICK); 
      ....................................................... 
      ....................................................... 
    } 

    OnClickListener onClickListner_BTN_CLICK = new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       int iTag = (Integer)v.getTag(); 

       switch (iTag) { 
       case TAG_IMAGE_BTN_1: 

        break; 
         case TAG_IMAGE_BTN_2: 

        break; 

         case TAG_IMAGE_BTN_3: 

        break; 

         case TAG_IMAGE_BTN_4: 

        break; 

       } 
      } 
     }; 


    } 

使用標籤值捕捉動作的優點, 1)如果我們使用R.id .......那麼我們就必須改變開關的情況下,如果值我們在xml文件中更改它 2)我們只能爲活動中的每個操作使用一個onClickListner。

+0

我真的很喜歡這個解決方案。謝謝!你能解釋我如何一起實現這個條件(questionArray == questionIndex1)。我需要得到兩個條件:按下哪個按鈕,並且該按鈕是正確的答案。謝謝! –

相關問題