我希望能够从EditText删除焦点。例如,如果键盘出现,用户用后退按钮隐藏它,我希望焦点和光标消失。怎样才能做到呢?
当前回答
当活动开始时隐藏键盘。在onCreate()中编写以下代码..
InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
清除焦点并从edittext.....中删除光标
editText.clearFocus();
editText.setCursorVisible(false);
其他回答
移除焦点但保持可聚焦:
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
EditText将失去焦点,但可以在新的触摸事件中再次获得焦点。
editText.setFocusableInTouchMode(true)
EditText将能够在用户触摸它时获得焦点。 当主布局(活动,对话框等)变得可见时,EditText不会自动获得焦点,即使它是布局中的第一个视图。
您可以使光标和焦点消失
edittext.clearFocus();
但检测时隐藏键盘是一项艰苦的工作。
当活动开始时隐藏键盘。在onCreate()中编写以下代码..
InputMethodManager imm = (InputMethodManager)
getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
清除焦点并从edittext.....中删除光标
editText.clearFocus();
editText.setCursorVisible(false);
将这两个属性添加到你的父布局(例如:线性布局,相对布局)
android:focusable="true"
android:focusableInTouchMode="true"
它会成功的:)
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件