Android:如何在多个图像按钮上设置监听器?

时间:2013-01-17 07:35:33

标签: java android events imagebutton click

首先,我只使用Andorid和Java一周,而且我是一个新手。

我需要知道用户点击了哪个按钮,然后将其与好的或坏的答案进行比较。

这是代码

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与按钮ID进行比较但我真的不知道如何实现它。感谢每一位帮助。

5 个答案:

答案 0 :(得分: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;
            }
    }
     

}

答案 1 :(得分: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
        }
    }
}

答案 2 :(得分: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))
                    {

                    }

                }
            });

我希望这会对你有所帮助

答案 3 :(得分:0)

你可以这样做: 首先,让你的活动实现onClick Listener:

public class MyActivity extends Activity implements View.OnClickListener{ 

然后将动作侦听器设置为按钮:

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

然后在监听器中,检查id:

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

答案 4 :(得分: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文件中更改它,我们必须更改switch case中的值 2)我们只能对活动中的每个动作使用一个onClickListner。