这是个扫码枪回车输入扫码内容的界面,常用于收银收款等场景
前期踩了很多坑,网上的资料也因为 Android 历史版本不同有各种兼容问题,最后总结了下
在无霸屏设置的 android 设备上使用如下方案可有效避免界面弹出软键盘和显示顶部状态栏问题,环境为 Android 7.1.2
屏蔽软键盘:自动聚焦
隐藏顶部状态:方案一 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()
}
}
如有问题或建议,欢迎大家评论区讨论指正!