博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Intercept back button from soft keyboard(从软键盘拦截后退按钮)
阅读量:5298 次
发布时间:2019-06-14

本文共 1374 字,大约阅读时间需要 4 分钟。

onKeyDown() and onBackPressed() doesn't work for this case. You have to use onKeyPreIme.

Initially, you have to create custom edit text that extends EditText. And then you have to implement onKeyPreIme method which controls KeyEvent.KEYCODE_BACK. After this, one back press enough for solve your problem. This solution works for me perfectly.

CustomEditText.java

public class CustomEditText extends EditText { Context context; public CustomEditText(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; } @Override public boolean onKeyPreIme(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { // User has pressed Back key. So hide the keyboard InputMethodManager mgr = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); mgr.hideSoftInputFromWindow(this.getWindowToken(), 0); // TODO: Hide your view as you do it in your activity } return false; }

In your XML

In your Activity

public class MainActivity extends Activity { private CustomEditText editText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (CustomEditText) findViewById(R.id.CEditText); } } 解决方案出版: http://stackoverflow.com/questions/3940127/intercept-back-button-from-soft-keyboard

转载于:https://www.cnblogs.com/aland/p/4936914.html

你可能感兴趣的文章
列表select如何传值给函数
查看>>
ElasticSearch(八) Bulk API
查看>>
优先级队列priority_queue
查看>>
9-----BBS论坛
查看>>
1089. Insert or Merge (25)-判断插入排序还是归并排序
查看>>
新手对web.xml的描述
查看>>
解决tomcat中jdk1.5运行日志相差8小时问题
查看>>
8086cpu中的标志寄存器与比较指令
查看>>
oj教程--排序算法(Java)
查看>>
Oracle数据库的一些简单sql问题
查看>>
Servlet中的Filter 过滤器的简单使用!
查看>>
实验2
查看>>
求解斐波那契数列模$p$意义下最短循环节
查看>>
Window7幻灯片字体显示混乱,难道真的是病毒么
查看>>
JavaScript的程序构成
查看>>
driver: Linux设备模型之input子系统具体解释
查看>>
golang基于etcd实现分布式锁(转)
查看>>
解决pod search出来的库不是最新
查看>>
钱币兑换问题(hd1284)
查看>>
1.Two Sum
查看>>