先看效果图
对于这个问题整的挺难受的,国内很多网上的解决办法,感觉真的不靠谱,最后看到一个帖子,是老外写的,解决了。一句话:复写Edittext;
不多说,上代码:
package com.app.test.testedittextproject;
import android.content.Context;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.widget.EditText;
/**
* Created by liumengqiang on 17/6/7.
*/
public class ActionEditText extends EditText {
public ActionEditText(Context context) {
super(context);
}
public ActionEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ActionEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
InputConnection connection = super.onCreateInputConnection(outAttrs);
int imeActions = outAttrs.imeOptions&EditorInfo.IME_MASK_ACTION;
if ((imeActions&EditorInfo.IME_ACTION_DONE) != 0) {
// clear the existing action
outAttrs.imeOptions ^= imeActions;
// set the DONE action
outAttrs.imeOptions |= EditorInfo.IME_ACTION_DONE;
}
if ((outAttrs.imeOptions&EditorInfo.IME_FLAG_NO_ENTER_ACTION) != 0) {
outAttrs.imeOptions &= ~EditorInfo.IME_FLAG_NO_ENTER_ACTION;
}
return connection;
}
}
然后在XML 的EditText中添加 imeOptions=“actionDone”;
<com.app.test.testedittextproject.ActionEditText
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Hello World!"
android:background="@color/colorPrimary"
android:imeOptions="actionDone"
android:layout_marginTop="50dp"/>
OK了!!!