1

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

4

5 回答 5

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

}

于 2013-01-17T07:42:30.200 回答
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
        }
    }
}
于 2013-01-17T07:47:13.893 回答
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))
                    {

                    }

                }
            });

我希望这能帮到您

于 2013-01-17T11:22:37.420 回答
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。

于 2013-01-17T09:23:51.100 回答
0

您可以这样做:首先,使您的活动实现 onClick 侦听器:

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;
   ....
   }
}
于 2013-01-17T07:42:09.043 回答