2013-08-18 163 views
5

好吧,花了數小時尋找解決方案後,我來到了這裏。當android:gravity =「center」隱藏時,EditText被鍵盤隱藏 - Android

我認爲這可能是Android的一個問題。

嘗試創建這個簡單的佈局。打開鍵盤,隱藏它,然後再次打開並且EditText被隱藏。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="300dp" 
     android:gravity="center" 
     android:inputType="phone" 
     android:maxLength="14" 
     android:text="some text" 
     android:textColor="#666666" 
     android:textSize="22sp" /> 

</RelativeLayout> 

現在,刪除android:gravity="center",一切正常! 在你問我之前,是的,我已經添加了android:windowSoftInputMode="adjustPan"

任何解決方案將不勝感激!謝謝

+0

如果將它放在scrollView中會發生什麼?那麼它也發生? –

+0

我已將我的RelativeLayout放入ScrollView中:不會更改任何內容,對不起 –

+0

整個'EditText'是隱藏的還是隻是字符的起始位置? – codeMagic

回答

2

我最近在Nexus 7手機上處理了這個問題。這種行爲在我們其他任何測試手機上都沒有發生。訣竅似乎是在關閉軟鍵盤之前更改焦點。鍵盤在3點處關閉 - 單擊完成按鈕,在編輯文本框外單擊,然後單擊後退按鈕。

首先,創建一個隱藏的元素吃你的焦點

<MyEditText 
      android:id="@+id/editHidden" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_below="@id/imageLogin" 
      /> 

我存儲在此,但它是一個有點難看......

@Override 
    protected void onResume() { 
     super.onResume(); 

     Utils.focusable = findViewById(R.id.editHidden); 

現在,當鍵盤焦點更改到你的隱藏元素關閉了。

public static void clearFocus() { 
     try { 
      if (focusable!=null) 
       focusable.requestFocus(); 
     } catch (Exception e) {} 
    } 

    public static void hideSoftKeyboard(View view) { 
     clearFocus(); 
     if (view!=null) { 
      try { 
       InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Activity.INPUT_METHOD_SERVICE); 
       inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
      } catch (Exception e) {} 
     } 
    } 

然後運行在斑點hideKeyboard功能鍵盤被關閉: 的EditText後退按鈕按下:

@Override 
    public boolean onKeyPreIme(int keyCode, KeyEvent event) 
    { 
     if(keyCode == KeyEvent.KEYCODE_BACK) 
     { 
      try { 
       Utils.clearFocus(); 
      } catch (Exception e) {} 
     } 
     return super.onKeyPreIme(keyCode, event); 
    } 

完成按鈕按下,重視這問題與發生任何的EditText框:

public static OnEditorActionListener getOnEditorActionListener() { 
     return new OnEditorActionListener() {   

      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if(actionId==EditorInfo.IME_ACTION_DONE){ 
        hideSoftKeyboard(v); 
       } 

       return false; 
      } 
     }; 
    } 

單擊文本框的外部 - 附加到onCreate();頁面上的所有元素。

公共靜態無效setupUI(查看視圖){

try { 
    //Set up touch listener for non-text box views to hide keyboard. 
    if(!(view instanceof EditText)) { 

     view.setOnTouchListener(new OnTouchListener() { 

      public boolean onTouch(View v, MotionEvent event) { 
       hideSoftKeyboard(v); 
       return false; 
      } 

     }); 
    } 

    //If a layout container, iterate over children and seed recursion. 
    if (view instanceof ViewGroup) { 

     for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 

      View innerView = ((ViewGroup) view).getChildAt(i); 

      setupUI(innerView); 
     } 
    } 
} catch (Exception e) {} 

}

這是相當混亂,但沒有解決問題,希望有雖然是更好的辦法。

+1

Wooow,謝謝,但這對我來說太棘手。我繞過了刪除android:gravity =「center」的問題 –