简体   繁体   中英

Disable Action listener for a while

I am newbie on Android Studio. I have a question. I am making a match cards application. After a failed match cards flipping back after 0,5 sec. But while this time, you can still check other cards. I want to disable that. I'm extends Button to a class. I'm posting both of this scripts. Thanks for your helps.

This is my Card Class.

public class kart extends Button {
    boolean acikMi = false;
    boolean cevrilebilir = true;
    int arkaPlanID;
    int onPlanID=0;
    Drawable on;
    Drawable d;

    public kart(Context context, int id) {
        super(context);
        setId(id);
        arkaPlanID = R.drawable.arka1;
        if(id%8==1)
            onPlanID =  R.drawable.c1;
        if(id%8==2)
            onPlanID =  R.drawable.c2;
        if(id%8==3)
            onPlanID =  R.drawable.c3;
        if(id%8==4)
            onPlanID =  R.drawable.c4;
        if(id%8==5)
            onPlanID =  R.drawable.c5;
        if(id%8==6)
            onPlanID =  R.drawable.c6;
        if(id%8==7)
            onPlanID =  R.drawable.c7;
        if(id%8==0)
            onPlanID =  R.drawable.c8;
        d = AppCompatDrawableManager.get().getDrawable(context,arkaPlanID);
        on = AppCompatDrawableManager.get().getDrawable(context, onPlanID);
        setBackground(d);


    }
    public void cevir(){
        if (cevrilebilir){
            if(!acikMi){
                setBackground(on);
                acikMi = true;
            }
            else{
                setBackground(d);
                acikMi=false;
            }

        }

    }

And this is my Java code.

    public class GameScreen extends AppCompatActivity {
    int sonKart = 0;
    int skor = 0;
    int hamle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_screen);
        Intent i = getIntent();
        final String s = i.getStringExtra("ad");
        TextView tv = (TextView) findViewById(R.id.textView);
        tv.setText(s);
        GridLayout gr = (GridLayout) findViewById(R.id.grid);
        kart kartlar[] = new kart[16];
        for (int j = 1; j <= 16; j++) {
            kartlar[j - 1] = new kart(this, j);
            kartlar[j-1].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hamle++;
                    final kart k = (kart)v;
                    k.cevir();
                    if (sonKart >0){
                        final kart k2 = (kart) findViewById(sonKart);
                        if (k2.onPlanID==k.onPlanID&&k2.getId()!=k.getId()){
                            k2.cevrilebilir=false;
                            k.cevrilebilir=false;
                            sonKart =0;
                            skor++;
                            TextView tv = (TextView) findViewById(R.id.textView3);
                            TextView tv2 = (TextView) findViewById(R.id.textView5);
                            tv.setText("Score: "+skor);
                            tv2.setText("Total moves: "+hamle);
                            if (skor==8){
                                Intent i = new Intent(GameScreen.this,scoreboard.class);
                                i.putExtra("ham",hamle);
                                i.putExtra("isim",s);
                                startActivity(i);
                                //oyun bitti

                            }



                        }
                        else{

                            Handler h = new Handler();
                            h.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    k2.cevir();
                                    k.cevir();

                                }
                            },500);
                            sonKart =0;
                            TextView tv2 = (TextView) findViewById(R.id.textView5);
                            tv2.setText("Total moves: "+hamle);

                        }
                    }
                    else{
                        sonKart = k.getId();
                    }

                }
            });
        }

        for (int j=0;j<16;j++){
            int rg =(int) (Math.random()*16);
            kart k = kartlar[rg];
            kartlar[rg] = kartlar [j];
            kartlar[j] = k;
        }

        for (int j = 0; j < 16; j++)
            gr.addView(kartlar[j]);




    }
}

禁用点击按钮

      button.setEnabled(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM