Android controls, double-click, press and other operations

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_39347584/article/details/102747074

Android controls, double-click, press and other operations @ TOC

Android controls, double-click, press and other operations

Double-click the control I wrote a callback class to write their own, then call directly in the main class on it, and a long press is even more simple, direct callback hanging long press on it, well, ado directly on the code!

Double-click callback class

/**
 * Created by Administrator on 2019/6/19 0019.
 * 双击方法监听
 */
public  abstract class DoubleClickListener implements View.OnClickListener {
    private static final long DOUBLE_TIME = 1000;
    private static long lastClickTime = 0;

    @Override
    public void onClick(View v) {
        long currentTimeMillis = System.currentTimeMillis();
        if (currentTimeMillis - lastClickTime < DOUBLE_TIME) {
            onDoubleClick(v);
        }
        lastClickTime = currentTimeMillis;
    }
    public abstract void onDoubleClick(View v);

}

Press and monitor function

imageDel.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(context,"长按成功",Toast.LENGTH_SHORT).show();
                return true;
            }
        });

to sum up

Well, it's that simple, let us at the same time of goodbye!

Guess you like

Origin blog.csdn.net/weixin_39347584/article/details/102747074