【问题标题】:How to stop Soft keyboard showing automatically when focus is changed (OnStart event)如何在焦点更改时停止软键盘自动显示(OnStart 事件)
【发布时间】:2011-03-07 15:39:52
【问题描述】:

我正在为使用 Android 2.2 的平板电脑进行开发。

我有一个用于新的可编辑实例的表单。当可编辑时,我想阻止用户编辑某些字段。我在我的onStart-事件中管理这个,设置txt.setFocusableInTouchMode(false)。 这会强制将焦点转移到我的表单中的下一个可聚焦的EditText(这很棒),但是在运行时,软键盘会自动出现在具有焦点的EditText 上。 有人知道如何阻止这种情况吗?

这是代码(在onStart 事件中调用):

private void PopulateFields(){
    TextView txtTitle = (TextView) this.findViewById(R.id.txtEquipmentEditTitle);
    AutoCompleteTextView txtMake = (AutoCompleteTextView) this.findViewById(R.id.autEquipmentEditMake);
    AutoCompleteTextView txtModel = (AutoCompleteTextView) this.findViewById(R.id.autEquipmentEditModel);
    EditText txtDesc = (EditText) this.findViewById(R.id.txtEquipmentEditDesc);
    TextView txtDeptKey = (TextView) this.findViewById(R.id.txtEquipmentEditDeptKey);
    EditText txtSerial = (EditText) this.findViewById(R.id.txtEquipmentEditSerialNo);
    EditText txtBarCode = (EditText) this.findViewById(R.id.txtEquipmentEditBarCode);

    txtTitle.setText("Edit Equipment");
    txtMake.setText(make);
    txtModel.setText(model);
    txtDesc.setText(desc);
    txtDeptKey.setText(Integer.toString(deptKey));
    txtSerial.setText(serial);
    txtBarCode.setText(barCode);
    txtMake.setEnabled(false);
    txtModel.setEnabled(false);
    txtDesc.setEnabled(false);
    txtMake.setClickable(false);
    txtMake.setFocusableInTouchMode(false);
    txtModel.setFocusableInTouchMode(false);
    txtDesc.setFocusableInTouchMode(false);
    txtMake.setFocusable(false);
    txtModel.setFocusable(false);
    txtDesc.setFocusable(false);
}

【问题讨论】:

    标签: android android-softkeyboard android-edittext


    【解决方案1】:

    也许这会对你有所帮助:

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    

    在首次创建或调用 Activity 时,将此方法放在您的 activityonCreate() 方法中。

    【讨论】:

    • 这解决了我在 xoom (Android 3.0.1) 上的问题。我将上面的行添加到我的 onCreate() 方法中。我仍然不确定什么是“软输入”。
    • 软输入是屏幕键盘。另一种选择是真正的物理键盘。
    • 这似乎对 Android 2.2 没有任何影响。SOFT_INPUT_STATE_ALWAYS_HIDDEN 也没有。
    • 您也可以通过在清单中的活动上添加 android:windowSoftInputMode="stateHidden" 来实现相同的效果
    【解决方案2】:

    我们可以通过以下方式隐藏device Keybord,这完全取决于情况的需要_

    First Way_

    EditText userId;
        /*
         * When you want that Keybord not shown untill user clicks on one of the EditText Field.
         */
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    

    Second Way_ InputMethodManager

        /*
         * When you want to use service of 'InputMethodManager' to hide/show keyboard
         */
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    
        inputManager.hideSoftInputFromWindow(userId.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    

    Third Way_

        /*
         * Touch event Listener
         * When you not want to open Device Keybord even when user clicks on concern EditText Field.
         */
        userId.setOnTouchListener(new OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int inType = userId.getInputType(); // backup the input type
                userId.setInputType(InputType.TYPE_NULL); // disable soft input
                userId.onTouchEvent(event); // call native handler
                userId.setInputType(inType); // restore input type
                userId.setFocusable(true);
                return true; // consume touch even
            }
        });
    

    代表以上所有方式_你也可以在applicationsmanifest`文件中定义windowSoftInputMode_

    <manifest ... >
     <application ... >
        <activity 
            android:windowSoftInputMode="stateHidden|stateAlwaysHidden"
            ... >
            <intent-filter>
               ...
            </intent-filter>
        </activity>
    
     </application>
    </manifest>
    

    希望对大家有所帮助!

    【讨论】:

      【解决方案3】:

      如果您想为特定的EditText 使用此功能,则可以使用。

      editText.setShowSoftInputOnFocus(false);

      【讨论】:

      • 所需的 API 21
      【解决方案4】:

      这就是答案: 在您的 AndroidManifest.xml 中为 Activity 添加一个属性:

              <activity android:name=".MyActivity" android:windowSoftInputMode="adjustPan"> </activity>
      

      【讨论】:

      • 我认为你完全错过了这个问题,这并没有隐藏键盘
      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 2017-10-23
      • 1970-01-01
      • 2011-11-09
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      相关资源
      最近更新 更多