如何设置延迟的Android的onClick功能 [英] How to set delay in Android onClick function
问题描述
) 我是在创造一个记忆游戏的过程。我的问题是,每当我点击第二次,我甚至不能看到切换按钮。需要明确的是 - 先点击切换切换按钮,这样我就可以看到它持有,第二次点击不同的切换按钮被suposed切换它的数量,显示我的号码,然后进行任何设置一个分数+1,如果数字是同样的,或者逆向他们回来,如果他们是不同的。
) 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.
下面是我为我的onClick功能使用code,我一直在思考把某种睡眠或延迟功能的somwhere在第二个假如块 - (如果(klikniecia == 2))
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)).
有关此主题的任何帮助将是AP preciated。
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;
}
}
}
}
}
推荐答案
而不是睡在onclick,你可以发布延迟的消息处理程序(具有相关的可运行),并将它更新UI。显然,这符合到你的应用程序的设计,使其工作,但基本的思路是这样的:
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
}
};
再从你的onClick发布延迟的消息处理程序,指定可运行执行。
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.
这篇关于如何设置延迟的Android的onClick功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!