Java Android TextView气泡onTouch

时间:2019-03-18 20:13:05

标签: java android textview ontouchlistener

在我的Android应用中,我有一个CardStack界面,每张卡都有一个标题。除非所说的卡片在前面,否则标题就是显示的所有内容,并且可以单击。因此,要选择这些卡之一,用户将需要单击TextView,但是要更改TextView中的文本,我想为用户提供LongClick TextView选项。

我尝试了setOnLongClickListener,但是这阻止了Click事件冒泡到CardStackAdapter并选择卡。

我尝试了setOnTouchListener与

if (event.getAction() == MotionEvent.ACTION_DOWN) 
    *open dialog slowly*
else if (event.getAction() == MotionEvent.ACTION_UP) 
    *close dialog*

但是,如果ACTION_DOWN块返回true,则不会冒泡。而且,如果返回false,则永远不会看到ACTION_UP块。

因为ACTION_UP不会使click事件冒泡到CardStackAdapter,所以我的最新尝试是让ACTION_UP块调用ACTION_DOWN事件并让ACTION_DOWN块缓慢打开对话框并返回true,或者停止对话框的打开并返回如果设置为false,则允许单击事件冒泡。

正在发生的是ACTION_DOWN调用的ClickUp不想冒泡。这是我的代码:

cardTitle.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP) {                            //If the Motion is ACTION_UP...
                if (dialogIsOpening) {
                    Log.i("OnTouchListener", "ACTION_UP: " + dialogIsOpening);
                    long downTime = SystemClock.uptimeMillis();
                    long eventTime = SystemClock.uptimeMillis() + 100;
                    float x = 0.0f;
                    float y = 0.0f;
                    int metaState = 0;
                    MotionEvent motionEvent = MotionEvent.obtain(
                            downTime,
                            eventTime,
                            MotionEvent.ACTION_DOWN,
                            x,
                            y,
                            metaState
                    );
                    v.dispatchTouchEvent(motionEvent);                                      //...invoke ACTION_DOWN.
                }

            } else if (event.getAction() == MotionEvent.ACTION_DOWN) {                  //Otherwise, if the Motion is ACTION_DOWN...
                Log.i("OnTouchListener", "ACTION_DOWN: " + dialogIsOpening);
                if (!dialogIsOpening) {                                                 //...if a dialog is not opening yet...
                    Log.i("OnTouchListener", "Showing dialog");
                    longPressHandler.postDelayed(openDialog, 500);            //...start to open it...
                    dialogIsOpening = true;                                             //...and remember that you're opening it.
                    return true;                                                        //Finally, consume the event so it doesn't bubble (not that important).

                } else {                                                                //If the Motion is ACTION_DOWN but a dialog is opening...
                    Log.i("OnTouchListener", "Hiding dialog");
                    longPressHandler.removeCallbacks(openDialog);                       //...stop the dialog from opening...
                    dialogIsOpening = false;                                            //...and remember the dialog isn't to open.
                    //Don't consume the event.  Let it bubble.
                }
            }
            return false;
        }
    });

感谢您的帮助。

0 个答案:

没有答案