2013-08-18 43 views
5

OK, sau nhiều giờ tìm kiếm giải pháp, tôi đã đến đây.EditText ẩn bằng bàn phím khi android: gravity = "center" - Android

Tôi nghĩ rằng đó có thể là sự cố với Android.

Cố gắng tạo bố cục đơn giản này. Mở bàn phím, ẩn nó, sau đó mở lại và EditText bị ẩn.

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

Bây giờ, hãy xóa android:gravity="center" và mọi thứ hoạt động! Trước khi bạn hỏi tôi, vâng tôi đã thêm android:windowSoftInputMode="adjustPan"

Bất kỳ giải pháp nào sẽ được đánh giá rất nhiều! Cảm ơn

+0

Điều gì sẽ xảy ra nếu bạn giữ nó bên trong scrollView? Sau đó, nó cũng xảy ra? –

+0

Tôi đã đặt RelativeLayout của mình bên trong một ScrollView: điều đó không thay đổi bất cứ điều gì, xin lỗi –

+0

Toàn bộ 'EditText' bị ẩn hay chỉ là nơi các ký tự bắt đầu? – codeMagic

Trả lời

2

Gần đây tôi đã xử lý sự cố này trên điện thoại Nexus 7. Hành vi không xảy ra trên bất kỳ điện thoại thử nghiệm nào khác của chúng tôi. Bí quyết có vẻ là thay đổi tiêu điểm TRƯỚC KHI bạn đóng softkeyboard. Bàn phím được đóng ở 3 điểm - nhấp vào nút Xong, nhấp vào bên ngoài hộp chỉnh sửa và nhấp vào nút quay lại.

Đầu tiên, tạo một yếu tố tiềm ẩn để ăn tập trung của bạn

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

tôi lưu trữ này, nhưng đó là một chút xấu xí ...

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

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

Bây giờ thay đổi tập trung vào yếu tố tiềm ẩn của bạn khi bàn phím đã đóng.

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) {} 
     } 
    } 

Sau đó chạy chức năng hideKeyboard trong những điểm bàn phím được đóng: EditText nút quay lại ép:

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

nút Done ép, đính kèm này cho bất kỳ hộp EditText vấn đề đang xảy ra với:

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; 
      } 
     }; 
    } 

Nhấp vào bên ngoài hộp văn bản - đính kèm với tất cả các phần tử trên trang inCreate();

public static void setupUI (Xem xem) {

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) {} 

}

Nó khá lộn xộn nhưng đã giải quyết vấn đề, hy vọng có một cách tốt hơn mặc dù.

+1

Wooow, cảm ơn bạn, nhưng điều đó quá khó đối với tôi. Tôi bỏ qua vấn đề xóa android: gravity = "center" –

Các vấn đề liên quan