简体   繁体   English

EditText,OnTouchListener和setSelection在第一次触摸时不起作用

[英]EditText, OnTouchListener and setSelection doesn't work on first touch

I've got the following code which fires every time my "redTime" EditText is touched. 我有以下代码,每次触摸我的“redTime”EditText时都会触发。

redTime.setOnTouchListener(new OnTouchListener() {

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

It is meant to keep the cursor on the right side of the EditText upon every touch. 它意味着在每次触摸时将光标保持在EditText的右侧。 The problem is that the line containing the "setSelection" method doesn't work upon the FIRST touch of the control. 问题是包含“setSelection”方法的行在第一次触摸控件时不起作用。 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). 也就是说,如果另一个控件具有焦点,并且我第一次触摸“redTime”控件,则触发该方法,但是光标保持在我触摸的位置(而不是右侧)。

How do I know the listener fires? 我如何知道听众的火灾? The "Log.i" call works, but not the cursor change. “Log.i”调用有效,但光标没有变化。 I suspect the "setSelection" call is working, but some later event is negating it. 我怀疑“setSelection”调用正在运行,但后来的一些事件正在否定它。

I tried a few things. 我试了几件事。 I tried consuming the event by returning TRUE in the listener. 我尝试通过在侦听器中返回TRUE来使用该事件。 Didn't work. 没工作。 I tried repeating the "setSelection" call in OnTouchListener, and OnFocusChanged as well. 我尝试在OnTouchListener和OnFocusChanged中重复“setSelection”调用。 Still doesn't work. 仍然无法正常工作。

I almost forgot. 我差点忘了。 Here is the XML for the control in question. 这是有问题的控件的XML。

<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;
        }
    });

Just put the edittext as like below 只需将edittext放在下面

   <EditText    
    android:id="@+id/from"
    android:focusable="false" 
    android:hint="@string/pickup_location"
    android:imeOptions="actionNext"  
    android:inputType="textNoSuggestions"    
    android:padding="10dp"
    android:textColor="@android:color/white" />

Set the ontouch listener in your activity 在活动中设置ontouch侦听器

 EditText et = (EditText)findViewById(R.id.et); et.setOnTouchListener(this)



@Override   public boolean onTouch(View v, MotionEvent event) {     
if (event.getAction() == MotionEvent.ACTION_UP)     
    switch (v.getId()) {            
            case R.id.from:
                //do your action
                break;

use this method: 使用此方法:

redTime.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction()==MotionEvent.ACTION_UP){  //check the event
                 redTime.requestFocus();  //keep focus on the EditText(redTime)
                 redTime.selectAll();  //select all text
            }

        }
        return true;
});

You could do 你可以做到

redTime.setOnTouchListener(new OnTouchListener() {

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

Before you call setSelection , that way redTime will have the focus when you do the selection. 在调用setSelection之前, redTime将在您进行选择时获得焦点。

To avoid the error do something like this: 为了避免错误,请执行以下操作:

yourEditText.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                yourEditText.setFocusable(true);
                yourEditText.requestFocus();
                yourEditText.setSelection(emailEditText.getText().length());
                break;
            case MotionEvent.ACTION_UP:
                v.performClick();
                break;
            default:
                break;
            }
            return true;

        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM