安卓:setOnClickListener到按钮。两次射击时pressed太快 [英] Android: setOnClickListener to Buttons. Firing twice when pressed too quick

查看:186
本文介绍了安卓:setOnClickListener到按钮。两次射击时pressed太快的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序目前,我正在做Android手机的一个问题。我有一个有两个按钮的活动。问题的关键是,如果我preSS两个按钮足够快(这是不是一个很好的用户行为),这两个按钮火,而我只想火(在很大部分)只是其中之一。这是一个code片段比如我的意思是:

I have a problem with an app i'm currently making for Android phones. I have an Activity that has two Buttons. The point is if i press both buttons fast enough (that's not a good user behaviour), those two buttons fire, while i want just to fire (at very most) only one of them. This a code fragment to example what I mean:

public class ActivityLevels extends Activity {

private boolean launched;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Set the content view of the screen
    setContentView(R.layout.levels);

    //Set lauched flag to false
    launched = false;

    //Initialize buttons
    Button loadLevel01 = (Button) findViewById(R.id.ButtonLoadLevel01);
    Button loadLevel02 = (Button) findViewById(R.id.ButtonLoadLevel02);

    //Set Button Listeners
    loadLevel01.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            launchGame(0);
        }
    });

    loadLevel02.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            launchGame(1);
        }

    });
}

private void launchGame(int level) {

    //Just launch the activity if !lanched
    if(!launched) {
        launched = true;
        //Start Activity
    }
}
}

我已经检查一个星期左右的互联网,并没有发现任何方法prevent这一点。至于我发现,貌似有人已经发布了类似的问题在这里:的问题20073:按钮的onClick监听器可以发射两次,即使按钮被监听器立即禁用

顺便说一句,我也tryed刚刚立即禁用按钮,在launchGame(INT)叫过,但没有取得任何东西。
这code是只是整个活动的改编片段。我认为这是确定明白我的意思,但如果你需要任何其他说明,请让​​我知道。

Btw, i've also tryed to disable the button just immediately before the "launchGame(int)" call, but not achieved anything. This code is just an adapted fragment of the whole activity. I think that's ok to understand what i mean, but if you need any other description, please let me know.

在此先感谢,和任何拼写错误对不起,我的英语不是很好的。

Thanks in advance, and sorry for any spelling mistakes, my english is not very good at all.

推荐答案

其实,我解决我的问题。问题的关键是使我的方法 launchGame(INT级) sychronized,所以它不能被访问一次它完成以前多了。通过这种方式,让我们说我们有从点击听众两个线程,因为我pressed键1和键两个非常非常快:具有同步的方法,它会被称为 lauchGame(1),完成动作,然后 launchGame(2)与推出标志布尔设置好的为真正

I actually solved my question. The point was to make my method launchGame(int level) sychronized, so it can't be accessed more than once before it finishes. This way, let's say we have two threads from click listeners, because i pressed the button one and button two very very quick: with a synchronized method, it will be called lauchGame(1), finish the action, and then launchGame(2) with the boolean launched flag setted to true.

这是我干了什么:

private void synchronized launchGame(int level) {

    //Just launch the activity if !lanched
    if(!launched) {
        launched = true;
        //Start Activity
    }
}

不管怎么说,非常感谢您的所有答案!

Anyways, many thanks for all of your answers!

这篇关于安卓:setOnClickListener到按钮。两次射击时pressed太快的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆