2016-01-22 83 views
3

我发现了一些这个问题的答案,但他们都没有为我工作。我在我的Fragment中有一个编辑文本,它在应用程序启动时启动。当这个片段打开时,软键盘也会弹出。我如何防止这种情况发生?这是我在我在我的片段onCreateView方法....关闭片段中的软键盘

 try { 
     InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
       Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(userName.getWindowToken(), 0); 
    }catch(Exception e) { 
     e.printStackTrace(); 
    } 
+0

do y ou把它修好了 –

+0

可以请你发布你的XML吗? –

回答

5

onCreateViewonActivityCreated试试这个。

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

我最近的项目,我使用的代码如下隐藏键盘布局,也许你可以试试。(我学习它从Wordpress-android源代码)

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_template_add_doc, container, false); 
    //hide the keyboard if it is visible 
    InputMethodManager imm = (InputMethodManager) getActivity() 
      .getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0); 
    return view; 
} 
1

请尝试以下逻辑来隐藏键盘自动打开。

尝试将您的编辑文本置于单独的线性布局中并设置android:focusableInTouchMode="true"。这将自动避免键盘自动打开。

<LinearLayout 
    android:id = "@+id/layout" 
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" 
    android:focusable = "true" 
    android:focusableInTouchMode = "true"> 

    <EditText 
     android:id = "@+id/edit_text" 
     android:layout_width = "match_content" 
     android:layout_height = "wrap_content"/> 
    </LinearLayout> 

或者如果上述失败,使用下面的代码以编程方式隐藏。把它写成一个单独的函数并在代码中调用它。

在创建视图后,在您的片段中调用此方法,如下所示。

@Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    hideKeyboard(); 
    } 

public void hideKeyboard() { 
    InputMethodManager inputMethodManager = (InputMethodManager) activity 
      .getSystemService(android.content.Context.INPUT_METHOD_SERVICE); 

    inputMethodManager.hideSoftInputFromWindow(
      activity.getCurrentFocus() 
        .getWindowToken(), 0); 
    } // hideKeyboard 

好运..!

0

这个工作对我来说,尝试这样

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 


hideKeyboard(getActivity()); 
} 

    public static void hideKeyboard(Context context) { 

      try { 
       InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 

       View view = ((Activity) context).getCurrentFocus(); 
       if (view != null) { 
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
0
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:background="@color/background_listview" 
android:orientation="vertical"> 

在主上布局使用此 设置可聚焦的真实和android:focusableInTouchMode真正

机器人:可调焦= “真”

机器人:focusableInTouchMode =” true“