6

我有一個EditText裏面的一個活動中的片段。如何在Android中完美顯示軟鍵盤?

我的動態佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@drawable/login_bg"> 
    ... 

    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    ... 

</RelativeLayout> 

我的動態配置在AndroidManifest.xml:

<activity 
     android:name="com.demo.LoginActivity" 
     android:configChanges="orientation|keyboardHidden" 
     android:launchMode="singleTop" 
     android:screenOrientation="portrait" 
     android:theme="@style/activityTheme" /> 

我的代碼使用在活動開始片段:

private void startFragment(BaseFragment fragment, String tag) { 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.fragment_container, fragment); 
    fragmentTransaction.addToBackStack(tag); 
    fragmentTransaction.commitAllowingStateLoss(); 
} 

我的片段佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/common_background_color_white" 
    android:orientation="vertical" 
    android:clickable="true" 
    android:paddingLeft="@dimen/email_common_padding_horizontal" 
    android:paddingRight="@dimen/email_common_padding_horizontal"> 

    ... 

    <com.example.widget.LineEditView 
     android:id="@+id/login_email_input" 
     style="@style/BaseEditText.LoginEditText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:focusable="true" 
    /> 

    ... 

</LinearLayout> 

我的自定義的Widget LineEditView是一個子類擴展RelativeLayout,而且佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/input" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:id="@+id/edit" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:focusable="true" 
     android:gravity="start|center_vertical" 
     android:background="@android:color/transparent" 
     android:textColor="@color/common_text_color_black" 
     android:maxLines="1" 
     android:textCursorDrawable="@drawable/common_cursor_background_orange" 
     android:textSize="@dimen/email_fields_text_size" 
     android:paddingBottom="@dimen/email_fields_text_padding_bottom"/> 

    <View 
     android:id="@+id/underline" 
     android:layout_below="@id/edit" 
     android:layout_width="match_parent" 
     android:layout_height="2px"/> 
</RelativeLayout> 

我想根據EditText上的的inputType屬性顯示軟鍵盤,並能隱藏容易。

什麼我都試過,但沒有工作或不完美:

1。根據Show keyboard for edittext when fragment starts可以顯示軟鍵盤,但不能輕易地掩蓋(甚至有時不能隱藏),並沒有根據的inputType屬性顯示鍵盤EditText上。

2.I添加以下代碼片段我:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container) { 
    mEditText = (EditText) rootView.findViewById(R.id.edit); 
    mEditText.requestFocus(); 
    mEditText.setFocusable(true); 
} 

@Override 
public void onResume() { 
    mEditText.postDelayed(mShowSoftInputRunnable, 400); 
    super.onResume(); 
} 

private Runnable mShowSoftInputRunnable = new Runnable() { 
    @Override 
    public void run() { 
     FragmentActivity activity = getActivity(); 
     if (activity == null) 
      return; 

     InputMethodManager input = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
     input.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT); 
    } 
}; 

,但它不能在我的片段顯示軟鍵盤。

我不能把EditText放到活動中,因爲它需要重構很多代碼。

有沒有人有想法來解決這個問題?

回答

16

這裏是我使用的代碼工作得很好的片段

public static void hideKeyboard(Context context) { 
    try { 
     ((Activity) context).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
     if ((((Activity) context).getCurrentFocus() != null) && (((Activity) context).getCurrentFocus().getWindowToken() != null)) { 
      ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(((Activity) context).getCurrentFocus().getWindowToken(), 0); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public static void showKeyboard(Context context) { 
    ((InputMethodManager) (context).getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 
} 
+0

謝謝你,你的回答幫了我很多。 – Waylent

+0

非常感謝。這一個完美的作品!嘗試了所有其他的東西!沒有很好地工作! –

+0

你們兩個都比歡迎。此代碼是我用於在android中顯示或隱藏軟鍵盤的最佳代碼。 – apmartin1991

2

hideSoftKeyboard

在片段需要調用此要隱藏鍵盤

hideSoftKeyboard(getActivity()); 

呼叫功能

private void hideSoftKeyboard(Activity activity) 
{ 
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
    inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); 
} 

showSoftKeyboard

您在frament稱之爲作秀鍵盤傳遞的EditText

EditText edittext=(EditText) findViewById(R.id.edittext); 
showSoftKeyboard(edittext); 

調用這個函數

public void showSoftKeyboard(View view) { 
    InputMethodManager inputMethodManager = (InputMethodManager)getActivity(). getSystemService(Activity.INPUT_METHOD_SERVICE); 
    view.requestFocus(); 
    inputMethodManager.showSoftInput(view, 0); 
} 
+0

謝謝你的幫助。但是你的showSoftKeyboard方法可能需要在view.postDelayed()中執行,否則它不能工作。而你的hideSOftKeyboard方法可能需要添加((Activity)上下文).getWindow()。 setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); – Waylent

相關問題