第一次触摸时,EditText,OnTouchListener和setSelection不起作用 [英] EditText, OnTouchListener and setSelection doesn't work on first touch

查看:81
本文介绍了第一次触摸时,EditText,OnTouchListener和setSelection不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,每次触摸"redTime" EditText时都会触发.

I've got the following code which fires every time my "redTime" EditText is touched.

redTime.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.i("click", "onMtouch");
            redTime.setSelection(redTime.getText().length());
            return false;
        }
    });

这是指每次触摸时都将光标保持在EditText的右侧.问题是,包含"setSelection"方法的行在第一次触摸控件时不起作用.也就是说,如果另一个控件具有焦点,并且我第一次触摸"redTime"控件,则将触发该方法,但是光标仍停留在我触摸的位置(而不是右侧).

It is meant to keep the cursor on the right side of the EditText upon every touch. The problem is that the line containing the "setSelection" method doesn't work upon the FIRST touch of the control. That is, if another control has focus, and I touch the "redTime" control for the first time, the method is fired, but the cursor remains at the location I touched (not the right side).

我怎么知道听众会开火?"Log.i"调用有效,但光标更改不起作用.我怀疑"setSelection"调用正在工作,但是稍后发生了一些否定事件.

How do I know the listener fires? The "Log.i" call works, but not the cursor change. I suspect the "setSelection" call is working, but some later event is negating it.

我尝试了几件事.我尝试通过在侦听器中返回TRUE来使用事件.没用我尝试在OnTouchListener和OnFocusChanged中重复"setSelection"调用.仍然不起作用.

I tried a few things. I tried consuming the event by returning TRUE in the listener. Didn't work. I tried repeating the "setSelection" call in OnTouchListener, and OnFocusChanged as well. Still doesn't work.

我差点忘了.这是有问题的控件的XML.

I almost forgot. Here is the XML for the control in question.

<EditText
            android:id="@+id/redEditText"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:ellipsize="end"
            android:gravity="right"
            android:inputType="time"
            android:text="@string/zeroTime"
            android:textColor="#ff0000"
            android:textSize="32sp" >
        </EditText>

推荐答案

我最近遇到了这个问题,无法正常工作,所以最终这样做:

I ran into this issue recently and couldn't get anything to work, so ended up doing this:

    setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            postDelayed(new Runnable() {
                @Override
                public void run() {
                    setSelection(getText().length());
                }
            }, 50);
            return false;
        }
    });

这篇关于第一次触摸时,EditText,OnTouchListener和setSelection不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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