问题
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;
}
});
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).
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.
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.
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>
回答1:
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;
}
});
回答2:
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;
}
});
回答3:
Just put the edittext as like below
<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
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;
回答4:
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.
回答5:
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;
});
来源:https://stackoverflow.com/questions/24335853/edittext-ontouchlistener-and-setselection-doesnt-work-on-first-touch