检测EditText上的触摸,但不会中断它?
我需要知道用户在我的活动中触摸/点击/点击edittext。
我怎样才能做到这一点,而不中断的事件,所以键盘仍然显示正确?
(我需要了解它之前,操作系统显示键盘…如果可能的话)
txtEdit.setOnTouchListener(new View.OnTouchListener(){ public boolean onTouch(View view, MotionEvent motionEvent) { // your code here.... getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); return false; } });
View.OnTouchListener onTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_UP){ ... }
你应该可以通过附加一个OnClickListener到你的EditText来做到这一点。 如果您担心在OnClickListener中阻塞UI线程,则可以生成一个新的线程并在其中执行工作 – 但是,如果这样做,则不能保证在键盘显示之前完成工作。
较不详细
与Mathias Conradt的方法相同,但使用kotlin:
txtEdit.setOnTouchListener({ view, motionEvent -> // your code here.... false })
这行getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
由于您点击editText视图时显示的键盘是不必要的。
- 无法导入AndroidJUnit4和ActivityTestRule
- SocketException:sendto失败:EBADF(坏文件描述符)
- org.gradle.api.tasks.TaskExecutionException:执行失败的任务’:app:compileDebugKotlin’
- 如何在Android中使用SIM卡获取手机号码使用kotlin语言编程?
- 如何在应用程序中打开浏览器中与webview url无关的链接
- Android Studio 3 canary&Kotlin
- 获取权限拒绝例外
- Android Studio Kotlin dataSnapshot.getValue错误
- Kotlin绑定视图:如何区分视图variables和局部variables?