2013-09-24 51 views
109

我在清单中有一个带有android:windowSoftInputMode="stateVisible"的Edittext。现在我开始活动时会显示键盘。如何隐藏它?我不能使用android:windowSoftInputMode="stateHidden,因为当键盘可见时,最小化应用程序并恢复它,键盘应该是可见的。 我试着用如何在活动开始时隐藏软键盘

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

,但没有奏效。

回答

175

使用以下函数来显示/隐藏键盘:

/** 
* Hides the soft keyboard 
*/ 
public void hideSoftKeyboard() { 
    if(getCurrentFocus()!=null) { 
     InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
    } 
} 

/** 
* Shows the soft keyboard 
*/ 
public void showSoftKeyboard(View view) { 
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
    view.requestFocus(); 
    inputMethodManager.showSoftInput(view, 0); 
} 
+4

Context.INPUT_METHOD_SERVICE对于那些在片段或不在主要活动等 –

+1

不适用于Nexus 6P Android 6.0.1。 – GeneralKimi

+0

@GeneralKimi:我也喜欢你的问题 –

276

在AndroidManifest.xml:

<activity android:name="com.your.package.ActivityName" 
     android:windowSoftInputMode="stateHidden" /> 

,或者尝试

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

请检查this

+1

您是否使用过getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); – Neenu

+2

感谢'android:windowSoftInputMode =“stateHidden”' –

+1

其实在防止关注编辑文本方面也有很好的答案http://stackoverflow.com/questions/4668210/automatic-popping-up-keyboard-on-start-活动 –

20

试试这个:

<activity 
    ... 
    android:windowSoftInputMode="stateHidden|adjustResize" 
    ... 
> 

this一个了解更多详情。

1

尝试这一个也

Ed_Cat_Search = (EditText) findViewById(R.id.editText_Searc_Categories); 

Ed_Cat_Search.setInputType(InputType.TYPE_NULL); 

Ed_Cat_Search.setOnTouchListener(new View.OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) { 
     Ed_Cat_Search.setInputType(InputType.TYPE_CLASS_TEXT); 
     Ed_Cat_Search.onTouchEvent(event); // call native handler 
     return true; // consume touch even 
    } 
}); 
10

要隐藏在新建活动的时间softkeyboard启动或onCreate()onStart()等您可以使用下面的代码:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
4

添加下面的文本到你的xml文件。

<!--Dummy layout that gain focus --> 
      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:focusable="true" 
       android:focusableInTouchMode="true" 
       android:orientation="vertical" > 
      </LinearLayout> 
2

将这个代码的Java文件和传递参数为对象的EditText,

private void setHideSoftKeyboard(EditText editText){ 
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); 
} 
1

这是我做过什么:

yourEditText.setCursorVisible(false); //This code is used when you do not want the cursor to be visible at startup 
     yourEditText.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       v.onTouchEvent(event); // handle the event first 
       InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
       if (imm != null) { 

        imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // hide the soft keyboard 
        yourEditText.setCursorVisible(true); //This is to display cursor when upon onTouch of Edittext 
       } 
       return true; 
      } 
     }); 
1

试试这个。

首先在您的可搜索的xml字段(名称和提示等)放@string而不是字面字符串。

则方法onCreateOptionsMenu,它必须有一个ComponentName对象与你的包名和已完成的类名(含包名) - 在它具有SearchView构件外壳活动是一样的节目搜索结果使用getComponentName(),作为谷歌Android开发人员说。

我尝试了很多解决方案,经过很多工作,这个解决方案对我来说很有用。

28

只需将两个属性添加到editText的父视图。

android:focusable="true" 
android:focusableInTouchMode="true" 
+0

真棒技巧+1。 – Ironman

4

我希望这会工作,我尝试了很多方法,但这一个在fragments工作。只需将此行放入onCreateview/init。

getActivity().getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
2

您可以在AndroidManifest.xml中设置配置

例子:

<activity 
    android:name="Activity" 
    android:configChanges="orientation|keyboardHidden" 
    android:theme="@*android:style/Theme.NoTitleBar" 
    android:launchMode="singleTop" 
    android:windowSoftInputMode="stateHidden"/> 
23

将这个清单中的活动标签

android:windowSoftInputMode="stateHidden" 
1

使用下面的代码里面隐藏软键盘第一次启动活动时

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