Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何仅允许长时间单击可聚焦_Android - Fatal编程技术网

Android 如何仅允许长时间单击可聚焦

Android 如何仅允许长时间单击可聚焦,android,Android,因此,我想做的是有一个列表,在listview中,每一行都会有一些编辑文本,如果用户长时间单击其中一个,那么我可以显示软键盘,并在完成后进行验证。我不知道如何做到这一点,因为我知道你可以在全球范围内使编辑文本不可编辑,但我希望它只能通过长时间点击编辑。我需要查看源代码并为此创建新的视图类型吗?有更简单的解决方案吗?将EditText的所有focusableInTouchMode设置为false 将以下字段添加到类中 protected TimeAnimator timer = new TimeA

因此,我想做的是有一个列表,在listview中,每一行都会有一些编辑文本,如果用户长时间单击其中一个,那么我可以显示软键盘,并在完成后进行验证。我不知道如何做到这一点,因为我知道你可以在全球范围内使编辑文本不可编辑,但我希望它只能通过长时间点击编辑。我需要查看源代码并为此创建新的视图类型吗?有更简单的解决方案吗?

将EditText的所有focusableInTouchMode设置为false

将以下字段添加到类中

protected TimeAnimator timer = new TimeAnimator();
protected int longPressTime = 1000; //in millisecond
protected int moveTol = 50; //in pixel
protected float startX;
protected float startY;
protected View pressedView;
将具有以下onTouch方法的OnTouchListener添加到所有 编辑文本

public boolean onTouch(View v, MotionEvent e){
    switch(e.getActionMasked()){
        case MotionEvent.ACTION_DOWN:
            timer.start();
            startX = e.getX();
            startY = e.getY();
            pressedView = v;
            break;
        case MotionEvent.ACTION_MOVE:
            if(Math.abs(startX-e.getX()) > moveTol || Math.abs(startY-e.getY()) > moveTol){
                timer.end(); //Abandon this press because it moves beyond tolerance
            }
            break;
        case MotionEvent.ACTION_UP:
            timer.end();
            break;
        default: break;
    }
    return true;
}
使您的类实现TimeListener并添加以下onTimeUpdate方法

public void onTimeUpdate(TimeAnimator timer, long t, long dt){
    if(t >= longPressTime){
        timer.end();
        pressedView.requestFocus();
    }
}
将此行添加到类的构造函数中

timer.setTimeListener(this);

如果requestfocus已经这样做了,为什么不添加一个onlongclicktouchlistener和requestfocus呢?因为我不知道有一个onlongclicktouchlistener。这可能是个好办法。