Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何在多个图像按钮上设置侦听器?_Java_Android_Events_Imagebutton_Click - Fatal编程技术网

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

Java Android:如何在多个图像按钮上设置侦听器?,java,android,events,imagebutton,click,Java,Android,Events,Imagebutton,Click,首先,我只使用android和Java一个星期,我完全是个新手 我需要知道用户点击了哪个按钮,然后将其与好的或坏的答案进行比较 这是密码 protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.questions

首先,我只使用android和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进行比较,但我真的不知道如何实现它。非常感谢您的帮助。

您可以这样做: 首先,让您的活动实现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;
   ....
   }
}
试着这样做:

}

写下面的代码

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
        }
    }
}

你也可以这样做。您必须使用一些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。

使用此简化代码

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))
                    {

                    }

                }
            });
ImageButton按钮1、按钮2、按钮3、按钮4;
ImageButton imagebuttons[]={button1,button2,button3,button4};
int-id[]={R.id.imageButton1,R.id.imageButton2,R.id.imageButton3,R.id.imageButton4};

对于(final int i=0;看。这很好,但是如何在每种情况下比较它?如果(questionArray==questionIndex1)如何在switch语句中在每种情况下比较它?如果(questionArray==questionIndex1)我非常喜欢这个解决方案。谢谢!你能解释一下如何一起实现这个条件(questionArray==questionIndex1)。我需要得到两个正确的条件:哪个按钮被按下,那个按钮是正确的答案。谢谢!谢谢!这帮助很大。你能解释一下如何一起实现这个条件吗(questionArray==questionIndex1)我需要两个条件:哪个按钮被按下,那个按钮是正确的答案。谢谢!这是你需要的吗?这个代码对你有用吗?请给我回电话
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))
                    {

                    }

                }
            });