如何知道在10个按钮中首次单击哪个按钮a

时间:2013-12-29 06:23:06

标签: java android

我正在android中创建一个游戏,其中将有10个洗牌数字,用户必须记住,然后逐个点击1-10。 我想知道是否有任何功能或方式在Android中知道哪个按钮是第一次点击,然后我可以设置这个值..thankyou。 我试图创建一个函数来计算点击的按钮数量,然后增加它的值以匹配我的按钮:

CODE ::

 public void settingFirstPressedButton (int buttonPressed)
        {
        if (firstPressedButton == -1)   //when firstPressedButton = -1 it means no button was pressed
          firstPressedButton = buttonPressed;
        }



then:

b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(firstPressedButton==-1){
                if(b1.getText()=="1"){
                    b1.setBackgroundColor(Color.GREEN);
                    break;
                }}

                else {
                    if(firstPressedButton==0){
                        if(b1.getText()=="2"){
                            b1.setBackgroundColor(Color.GREEN);
                            break;
                        }
                        }


                        else {
                            if(firstPressedButton==1){
                                if(b1.getText()=="3"){
                                    b1.setBackgroundColor(Color.GREEN);
                                    break;

                                }       }




                    else {
                        if(firstPressedButton==2){
                            if(b1.getText()=="4"){
                                b1.setBackgroundColor(Color.GREEN);
                                break;
                            }


                        }


                        else {
                            if(firstPressedButton==3){
                                if(b1.getText()=="5"){
                                    b1.setBackgroundColor(Color.GREEN);
                                    break;
                                }
                            }

                        else {
                            if(firstPressedButton==4){
                                if(b1.getText()=="6"){
                                    b1.setBackgroundColor(Color.GREEN);
                                    break;
                                }
                            }



                            else {
                                if(firstPressedButton==5){
                                    if(b1.getText()=="6"){
                                        b1.setBackgroundColor(Color.GREEN);
                                        break;
                                    }
                                }
                            else {
                                if(firstPressedButton==6){
                                    if(b1.getText()=="7"){
                                        b1.setBackgroundColor(Color.GREEN);
                                        break;
                                    }   
                        }

                                else {
                                    if(firstPressedButton==7){
                                        if(b1.getText()=="8"){
                                            b1.setBackgroundColor(Color.GREEN);
                                            break;
                                        }


                                        else {
                                            if(firstPressedButton==8){
                                                if(b1.getText()=="9"){
                                                    b1.setBackgroundColor(Color.GREEN);
                                                    break;
                                                }}

                                            else {
                                                if(firstPressedButton==9){
                                                    if(b1.getText()=="10"){
                                                        b1.setBackgroundColor(Color.GREEN);
                                                        break;

1 个答案:

答案 0 :(得分:3)

您可以使用自己的Activity作为OnClickListener。例如

public class MyActivity extends Activity implements View.OnClickListener {

     @Override
     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        f1 =(Button) findViewById(R.id.f1);
        f2 =(Button) findViewById(R.id.f2);
        :
        :
        f1.setOnClickListener(this);
        f2.setOnClickListener(this);
        :
        :
        :
     }


}

你必须实现onClick方法并切换view.getId():

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.f1:
         // your click action
        break;
    case R.id.f2:
         // your click action
        break;
            :
            :
    }
}

如果你想要按钮阵列那么

Button btns[]=new Button[10];
for (int i = 0; i < 10; i++) {
        btns[i] = new Button(this);
        btns[i].setText(""+i);
    }

如果你想让他们所有人做同样的事情,你可以使用for循环遍历数组,如下所示:

for(int i = 0; i< btns.length; i++){
    btns[i].setOnClickListener(new OnClickListener() {
        public void onClick(View v){
            //do something
            Log.d("Button click","Value " +  btns[i].getText() ;
        }
    });
}