点击EditText后禁止弹出输入法,看了网上很多写法都不太理想,还是看看官方是怎么写的吧该代码摘录于4.4拨号盘源码:
重写EditText.的onTouchEvent
Demo地址:http://download.youkuaiyun.com/detail/wds1181977/7509257
import android.content.Context;
import android.graphics.Rect;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
/**
* EditText which suppresses IME show up.
*/
public class DigitsEditText extends EditText {
public DigitsEditText(Context context, AttributeSet attrs) {
super(context, attrs);
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final boolean ret = super.onTouchEvent(event);
// Must be done after super.onTouchEvent()
final InputMethodManager imm = ((InputMethodManager) getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE));
if (imm != null && imm.isActive(this)) {
imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
}
return ret;
}
}
本文介绍了一种在使用EditText时禁止输入法自动弹出的方法,通过重写onTouchEvent并结合InputMethodManager来实现。示例代码来源于4.4拨号盘源码。
999

被折叠的 条评论
为什么被折叠?



