如何停止当焦点改变时自动显示软键盘(OnStart事件)

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

我有一个表单,我正在使用新的和可编辑的实例。 当可编辑时,我想停止用户编辑某些领域。 我在onStart -event中pipe理这个,设置txt.setFocusableInTouchMode(false) 。 这强制了焦点到我的窗体中的下一个可调焦的EditText (这太棒了),但是在运行时,软键盘自动出现在带有焦点的EditText上。 有人知道如何阻止这个?

下面是代码(在onStart事件中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); } 

Solutions Collecting From Web of "如何停止当焦点改变时自动显示软键盘(OnStart事件)"

也许这会帮助你:

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

我们可以通过完全依赖于情况的需要来隐藏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 } }); 

代表上面的所有方式,您还可以在应用程序s清单文件windowSoftInputMode中定义windowSoftInputMode

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

我希望这将有助于一切!

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

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