如何在Android onClick函数中设置延迟

发布于 2024-10-02 18:57:16 字数 1267 浏览 0 评论 0原文

) 我正在创建一个记忆游戏。我的问题是,每当我第二次单击时,我什至看不到切换按钮。需要明确的是 - 第一次单击会切换切换按钮,这样我就可以看到它所保存的数字,第二次单击不同的切换按钮应该会切换它,显示数字,然后继续设置分数+1(​​如果数字是)相同,或者如果不同则再次反转。

下面是我用作 onClick 函数的代码,我一直在考虑在第二个“if 块”中的某个位置放置某种睡眠或延迟函数 - (if(klikniecia ==2))。

任何有关此主题的帮助将不胜感激。

public void onClick(View view) {
for (int i = 0; i < karta.length; i++){
    if (view == karta[i]){
        karta[i].setEnabled(false);
        klikniecia++;
        if (klikniecia == 1){
            kartaID[0]=i;
            kartaWartosc[0]=listaKart.get(i);

        }
        if (klikniecia == 2){
            kartaID[1]=i;
            kartaWartosc[1]=listaKart.get(i);

            //i think, about setting a delay here, so i can see both of the cards, regardles if the're the same or not before reverting them.

            if (czyPara()){
                karta[kartaID[0]].setEnabled(false);
                karta[kartaID[1]].setEnabled(false);
                klikniecia=0;
            }
            else{

                karta[kartaID[0]].setEnabled(true);
                karta[kartaID[0]].toggle();
                karta[kartaID[1]].setEnabled(true);
                karta[kartaID[1]].toggle();
                klikniecia=0;

            }
        }

    }

}

}

)
I'm in a process of creating a memory game. My problem is that whenever i click for the second time, i can't even see toggled button. To be clear - first click toggles the togglebutton, so i can see the number it holds, the second click on a different togglebutton is suposed to toggle it, show me the number and then proceed to either set a score +1 if numbers are the same, or reverse them back again if they're different.

Below is the code that i use as my onClick function, i've been thinking about putting some kind of sleep or delay function somwhere in the second "if block" - (if(klikniecia ==2)).

Any help on this topic would be appreciated.

public void onClick(View view) {
for (int i = 0; i < karta.length; i++){
    if (view == karta[i]){
        karta[i].setEnabled(false);
        klikniecia++;
        if (klikniecia == 1){
            kartaID[0]=i;
            kartaWartosc[0]=listaKart.get(i);

        }
        if (klikniecia == 2){
            kartaID[1]=i;
            kartaWartosc[1]=listaKart.get(i);

            //i think, about setting a delay here, so i can see both of the cards, regardles if the're the same or not before reverting them.

            if (czyPara()){
                karta[kartaID[0]].setEnabled(false);
                karta[kartaID[1]].setEnabled(false);
                klikniecia=0;
            }
            else{

                karta[kartaID[0]].setEnabled(true);
                karta[kartaID[0]].toggle();
                karta[kartaID[1]].setEnabled(true);
                karta[kartaID[1]].toggle();
                klikniecia=0;

            }
        }

    }

}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

怪我闹别瞎闹 2024-10-09 18:57:16

您可以将延迟消息发布到处理程序(带有关联的可运行程序)并让它更新 UI,而不是在 onclick 中休眠。显然,将其融入到应用程序的设计中并使其正常工作,但基本思想是这样的:

//Here's a runnable/handler combo
private Runnable mMyRunnable = new Runnable()
{
    @Override
    public void run()
    {
       //Change state here
    }
 };

然后从 onClick 向处理程序发布一条延迟消息,指定要执行的可运行对象。

Handler myHandler = new Handler();
myHandler.postDelayed(mMyRunnable, 1000);//Message will be delivered in 1 second.

根据您的游戏的复杂程度,这可能并不完全是您想要的,但它应该给您一个开始。

Instead of sleeping in the onclick, you could post a delayed message to a handler (with associated runnable) and have it update the UI. Obviously fit this into the design of your app and make it work, but the basic idea is this:

//Here's a runnable/handler combo
private Runnable mMyRunnable = new Runnable()
{
    @Override
    public void run()
    {
       //Change state here
    }
 };

Then from onClick you post a delayed message to a handler, specifying that runnable to be executed.

Handler myHandler = new Handler();
myHandler.postDelayed(mMyRunnable, 1000);//Message will be delivered in 1 second.

Depending on how complicated your game is, this might not be exactly what you want, but it should give you a start.

善良天后 2024-10-09 18:57:16

正确工作:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 100ms
  }
}, 100);

Correctly work:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
  @Override
  public void run() {
    //Do something after 100ms
  }
}, 100);
溺深海 2024-10-09 18:57:16

你永远不应该在 UI 线程中休眠(默认情况下,你的所有代码都在 UI 线程中运行)——它只会使 UI 冻结,不会让某些内容发生更改或完成。我无法提出更多建议,因为我不明白代码逻辑。

You never should sleep in UI thread (by default all your code runs in UI thread) - it will only make UI freeze, not let something change or finish. I can't suggest more because I don't understand code logic.

梦过后 2024-10-09 18:57:16

不要在 UI 线程中休眠。您需要另一个线程来查找来自 UI 线程的“唤醒并等待”消息。然后,第二个线程可以在正常的睡眠调用后进行隐藏。然后,您可以在下次需要隐藏某些内容时保留该线程,或者在每次需要另一次延迟时杀死它并启动一个新线程。

我相信有一些“postFoo”函数在这种情况下可能有用(从 UI 线程外部触发 UI 事件)。

Do not sleep in the UI thread. You need another thread that will look for a "wake up and wait" message from your UI thread. That second thread could then do your hiding after a normal sleep call. You could then keep the thread around for the next time you need to hide something, or kill it and whip up a new one each time you need another delay.

I believe there are some "postFoo" functions that might be useful in this context (triggering UI events from outside the UI thread).

假装不在乎 2024-10-09 18:57:16
b.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        final ProgressDialog myPd_ring=ProgressDialog.show(MainActivity.this, "confident checker", "calculating please wait..", true);
        myPd_ring.setCancelable(true);
        new Thread(new Runnable() {  
              public void run() {
                    // TODO Auto-generated method stub


                    try
                    {
                          Thread.sleep(5000);


                    }
                    catch(Exception e){}
                    b.dismiss();
              }
        }).start();


        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
          public void run() {
              Toast.makeText(getApplicationContext(), "today your confident level is 90% ",
                        Toast.LENGTH_LONG).show();

          }
        }, 5000);


    }
});
b.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        final ProgressDialog myPd_ring=ProgressDialog.show(MainActivity.this, "confident checker", "calculating please wait..", true);
        myPd_ring.setCancelable(true);
        new Thread(new Runnable() {  
              public void run() {
                    // TODO Auto-generated method stub


                    try
                    {
                          Thread.sleep(5000);


                    }
                    catch(Exception e){}
                    b.dismiss();
              }
        }).start();


        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
          public void run() {
              Toast.makeText(getApplicationContext(), "today your confident level is 90% ",
                        Toast.LENGTH_LONG).show();

          }
        }, 5000);


    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文