Java EditText、OnTouchListener 和 setSelection 在第一次触摸时不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24335853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText, OnTouchListener and setSelection doesn't work on first touch
提问by mcsilvio
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. 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).
它旨在在每次触摸时将光标保持在 EditText 的右侧。问题是包含“setSelection”方法的行在第一次触摸控件时不起作用。也就是说,如果另一个控件具有焦点,并且我第一次触摸“redTime”控件,该方法将被触发,但光标仍停留在我触摸的位置(不是右侧)。
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.
我怎么知道监听器触发了?“Log.i”调用有效,但光标改变无效。我怀疑“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.
我尝试了几件事。我尝试通过在侦听器中返回 TRUE 来消耗该事件。没用。我尝试在 OnTouchListener 和 OnFocusChanged 中重复“setSelection”调用。还是不行。
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>
回答by TyMarc
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
将在您进行选择时获得焦点。
回答by ColossalChris
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;
}
});
回答by Raja Jawahar
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
在您的活动中设置 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;
回答by Raja Jawahar
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;
}
});
回答by ???? .?
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;
});