• 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()
        }
    
    }
    

    如有问题或建议,欢迎大家评论区讨论指正!

  • 相关阅读:
    已解决(Python最新xlrd库读取xlsx报错)SyntaxError: invalid syntax
    第09章_子查询
    Python + SQLAlchemy操作MySQL数据库(ORM)
    最新版手机软件App下载排行网站源码/App应用商店源码
    四川竹哲电商:抖店怎么修改经营类目?
    BATJ和字节跳动这些大厂的内部面试解析,面试重难点超出你的想象
    数据结构(C语言)——栈的两种实现方式
    springboot项目的properties文件配置项优先级
    Axios
    一文带你看懂Java中的Lock锁底层AQS到底是如何实现的
  • 原文地址:https://www.cnblogs.com/huelse/p/18458025