Android使用手势处理防止布局双击

huangapple go评论81阅读模式
英文:

Android Preventing Double Click On A Layout with handher

问题

以下是翻译好的内容:

有一个包含进度条的按钮布局。当点击按钮时,它会暂停几秒钟,然后进入下一个活动。
为了防止双重点击,我认为我需要将这些代码放在 OnclickListener 中:

private long mLastClickTime = 0;

if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
    return;
}
mLastClickTime = SystemClock.elapsedRealtime();

但我不知道放在哪里。

我的OnClickListener方法

btnContinue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        progress.setVisibility(View.VISIBLE);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (SystemClock.elapsedRealtime() - mLastClickTime < 1000){
                    return;
                }
                mLastClickTime = SystemClock.elapsedRealtime();

                progress.setVisibility(View.GONE);
                Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
                startActivity(intent);

            }
        }, 4500);
    }
});

我将监听器更改为:

btnContinue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        progress.setVisibility(View.VISIBLE);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
                    return;
                }

                progress.setVisibility(View.GONE);
                Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
                startActivity(intent);

            }
        }, 4500);
        mLastClickTime = SystemClock.elapsedRealtime();
    }
});

但是,当我多次点击按钮时,它会多次打开下一个活动。

英文:

There is a button layout that also has a progress bar inside. When the button is clicked, it pauses for a few seconds and enters the next activity.
To prevent double click i think i have to put these codes on OnclickListener

private long mLastClickTime = 0;

if (SystemClock.elapsedRealtime() - mLastClickTime &lt; 1000){
	return;
}
mLastClickTime = SystemClock.elapsedRealtime();

But i don't no where

My onClickListener method:

btnContinue.setOnClickListener(new View.OnClickListener() {
	@Override
	public void onClick(View v) {
	    progress.setVisibility(View.VISIBLE);
	    Handler handler = new Handler();
	    handler.postDelayed(new Runnable() {
	        @Override
	        public void run() {
	            if (SystemClock.elapsedRealtime() - mLastClickTime &lt; 1000){
	                return;
	            }
	            mLastClickTime = SystemClock.elapsedRealtime();

	            progress.setVisibility(View.GONE);
	            Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
	            startActivity(intent);

	        }
	    }, 4500);

	}
});

I change my listener to :

btnContinue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        progress.setVisibility(View.VISIBLE);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (SystemClock.elapsedRealtime() - mLastClickTime &lt; 1000) {
                    return;
                }

                progress.setVisibility(View.GONE);
                Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
                startActivity(intent);

            }
        }, 4500);
        mLastClickTime = SystemClock.elapsedRealtime();
    }
});

But when I clicked on the button several times, it opened the next activity several times

答案1

得分: 2

请移除处理程序并将您的逻辑代码放在那里,例如:

btnContinue.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        progress.setVisibility(View.VISIBLE);
        // 在此处放置您的逻辑代码
        mLastClickTime = SystemClock.elapsedRealtime();
    }
});
英文:

Please remove handler and put your logic code there e.g.

btnContinue.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            progress.setVisibility(View.VISIBLE);
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (SystemClock.elapsedRealtime() - mLastClickTime &lt; 1000){
                        return;
                    }            

                    progress.setVisibility(View.GONE);
                    Intent intent = new Intent(FirstPageMoneyTransfer.this, SecondPageMoneyTransfer.class);
                    startActivity(intent);

                }
            }, 4500);
          mLastClickTime = SystemClock.elapsedRealtime();
        }
    });

huangapple
  • 本文由 发表于 2020年7月26日 18:11:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63098746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定