2011-03-07 115 views
26

我正在开发使用Android 2.2的平板电脑。如何停止软键盘在焦点改变时自动显示(OnStart事件)

我有一个表单,我正在使用新的和可编辑的实例。在可编辑时,我想停止用户编辑某些字段。我在我的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); 
} 

回答

59

也许这将帮助你:

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

这固定我的问题的XOOM(安卓3.0.1)。我将上面的代码行添加到我的onCreate()方法中。我仍然不确定什么是“软输入”。 –

+2

软输入是屏幕键盘。另一种选择是一个真正的物理键盘。 –

+3

这似乎对Android 2.2没有任何影响。SOFT_INPUT_STATE_ALWAYS_HIDDEN也没有任何影响。 – Torben

9

我们可以通过以下方式这完全取决于需要的隐藏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 
     } 
    }); 

代表所有上述的ways_您还可以在应用程序中定义windowSoftInputModes manifest` _文件

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

</application> 
</manifest> 

我希望这会有所帮助!

0

这就是答案: 在你的AndroidManifest.xml中属性添加到活动:

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

我想你错过了这个问题它的整体,这不隐藏键盘 –

相关问题