2013-08-18 23 views
5

解決策を探すのに時間がたっても、私はここに来ました。アンドロイド:gravity = "center"のときにキーボードで編集されたテキストを編集

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" –

関連する問題