ANDROID:仅在双击时打开虚拟键盘?

问题描述

目前,当点击 TextField 或类似需要手动输入的地方时,android 虚拟键盘自动弹出,有没有办法防止它并仅在我手动双击 TextField 时设置它?

解决方法

参考:How to stop EditText from gaining focus at Activity startup in Android

ldap_values = {k: str(v).encode("utf-8") for k,v in ldap_values.items()}

这样做是让父布局默认获得焦点。因此只有当你按下两次时,焦点才会出现在文本视图上

,

在活动中

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_account_setting)
    //add for keyboard don't open automatically 
    this.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
     // and edit text enable false
     edittext.isEnabled = false

editText.setOnClickListener(new View.OnClickListener() {
        int count = 0;
        Handler handler = new Handler();
        Runnable runnable = () -> count = 0;

        @Override
        public void onClick(View v) {
            if (!handler.hasCallbacks(runnable))
                handler.postDelayed(runnable,500);
            if(count==2){
                 edittext.isEnabled = true
                 showKeyboard(editText,thiscontext!!)
                // 
            }
        }
    });

    //..............
    fun showKeyboard(editText: EditText,context: Context) {
        editText.post {
            editText.requestFocus()
            val imm = editText.context
                .getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            imm.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT)
        }
    }