27

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

我有一个用于新的可编辑实例的表单。当可编辑时,我想阻止用户编辑某些字段。我在我的onStart-event 中管理它,设置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);
}
4

4 回答 4

65

也许这会帮助你:

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

首次创建或调用活动时,将此方法放在onCreate()您的方法中。activity

于 2011-03-30T09:46:46.783 回答
13

我们可以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_ 输入法管理器

    /*
     * 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
        }
    });

代表上述所有方式_您也可以windowSoftInputMode在应用程序s清单`文件中定义_

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

 </application>
</manifest>

我希望这对所有人都有帮助!

于 2013-10-25T09:24:19.703 回答
6

如果你想要这个特定的,EditText这将起作用。

editText.setShowSoftInputOnFocus(false);

于 2018-04-17T17:51:11.487 回答
-1

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

        <activity android:name=".MyActivity" android:windowSoftInputMode="adjustPan"> </activity>
于 2015-01-29T20:08:15.027 回答