Android 扫码枪输入时屏蔽软键盘和顶部状态栏的解决方案
这是个扫码枪回车输入扫码内容的界面,常用于收银收款等场景
前期踩了很多坑,网上的资料也因为 Android 历史版本不同有各种兼容问题,最后总结了下
在无霸屏设置的 android 设备上使用如下方案可有效避免界面弹出软键盘和显示顶部状态栏问题,环境为 Android 7.1.2
屏蔽软键盘:自动聚焦 的 inputType 设置为 none
隐藏顶部状态:方案一 hideStatusBar 必须在 setContentView 之前,方案二在 styles 中设置 NoActionBar 具体可自行搜索
- AndroidManifest.xml
<activity
android:name=".MyActivity"
android:windowSoftInputMode="stateHidden"
android:exported="false" />- activity_my.xml
<EditText
android:id="@+id/scanInput"
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:focusedByDefault="true"
android:importantForAutofill="no"
android:inputType="none" />- MyActivity.kt
class MyActivity : AppCompatActivity() {
private lateinit var binding: ActivityMyBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMyBinding.inflate(layoutInflater)
hideStatusBar()
setContentView(binding.root)
hideSoftKeyboard()
}
override fun onResume() {
super.onResume()
hideSoftKeyboard()
hideActionBar()
}
private fun hideSoftKeyboard() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
this.currentFocus?.let { view ->
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.hideSoftInputFromWindow(view.windowToken, InputMethodManager.RESULT_HIDDEN)
}
}
private fun hideStatusBar() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
window.setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
private fun hideActionBar() {
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
actionBar?.hide()
}
}到此这篇关于Android 扫码枪输入时屏蔽软键盘和顶部状态栏的文章就介绍到这了,更多相关Android屏蔽软键盘和顶部状态栏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Android 中clipToPadding 和 clipChildren区别和作用
这篇文章主要介绍了Android 中clipToPadding 和 clipChildren区别和作用的相关资料,需要的朋友可以参考下2017-06-06
android中使用react-native设置应用启动页过程详解
这篇文章主要介绍了android中使用react-native设置应用启动页过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07
Android 蓝牙连接 ESC/POS 热敏打印机打印实例(蓝牙连接篇)
这篇文章主要介绍了Android 蓝牙连接 ESC/POS 热敏打印机打印实例(蓝牙连接篇),具有一定的参考价值,感兴趣的小伙伴们可以参考一下。2017-04-04
Android仿微信清理内存图表动画(解决surfaceView屏幕闪烁问题)demo实例详解
本文通过实例代码给大家讲解android仿微信清理内存图表动画(解决surfaceView屏幕闪烁问题)的相关资料,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下2016-09-09


最新评论