2013-03-21 94 views
0

我在我的活動中有一個編輯文本。當用戶觸摸該編輯文本時,鍵盤已打開。但是在所有的HTC設備中,打開鍵盤後,當用戶按下後退按鈕,而不是隻隱藏鍵盤時,我當前的活動結束並顯示以前的活動。如何解決這個問題?在所有其他三星手機,這工作正常。但不是在HTC設備。當在android中打開鍵盤時按下後退按鈕時,防止完成活動

+0

它的設計依賴..可能需要添加額外的代碼來控制有關隱藏鍵盤的HTC設置。 – Kiran 2013-03-21 04:31:10

回答

0

您可能會按兩次後退按鈕,因爲我做了同樣的事情,但沒有遇到類似的問題。我已經在三星和HTC設備上測試過我的代碼。

+0

雅這可能是真的..鍵可能太敏感,並被按兩次。 – Kiran 2013-03-21 04:34:40

0

那麼這可能只適用於你。您可以檢查如果鍵盤是onBackPressed活動廣場,如:

public void onBackPressed() { 
     final View activityRootView = findViewById(R.id.activityRoot); 
     activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); 
       if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard... 
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 
       } 
      } else { 
       super.onBackPressed(); 
      } 
     }); 
} 

,那麼你可以先取消鍵盤就像上面的代碼,然後終於再次按下背面你可以回去使用super.onBackPressed ();

0

坦率地說,我非常想說一句,讓平臺按照用戶預期的方式處理它。不過,最近我遇到了類似的問題,雖然它不是最理想的,但它確實允許您在後退按鈕到達鍵盤之前攔截它,並根據需要進行處理。

首先,無論你的ViewGroup佈局使用,將其覆蓋在類似這樣的方式的根本:

IMEInterceptLayout extends LinearLayout { 
    private OnBackPressedPreIMEListener listener; 

    public IMEInterceptLayout(Context c) { super(c); } 
    public IMEInterceptLayout(Context c, AttributeSet attrs) { super(c, attrs); } 
    public IMEInterceptLayout(Context c, AttributeSet attrs, int style) { super(c, attrs, style); } 

    @Override 
    public boolean dispatchKeyEventPreIme(KeyEvent event) { 
     switch(event.getKeyCode()) { 
      case KeyEvent.KEYCODE_BACK: 
       fireOnBackPressedPreIME(); 
       return true; 
      default: 
       return super.dispatchKeyEventPreIme(event); 
     } 
    } 

    public void setOnBackPressedPreIMEListener(OnBackPressedPreIMEListener listener) { 
     this.listener = listener; 
    } 

    private void fireOnBackPressedPreIME() { 
     if(listener != null) listener.onBackPressedPreIME(); 
    } 

    public interface OnBackPressedPreIMEListener { 
     public void onBackPressedPreIME(); 
    } 
} 

例如如果您使用的是RelativeLayout,請擴展而不是LinearLayout。在你的佈局,而不是普通的Android版本,使用該自定義視圖:

<LinearLayout 
    android:id="@+id/my_root_layout" 

內容視圖設置到此佈局後成爲

<com.my.packagename.IMEInterceptLayout 
    android:id="@+id/my_root_layout" 

然後,在你onCreate(),得到這個ViewGroup中的引用:

IMEInterceptLayout layout = (IMEInterceptLayout)findViewById(R.id.my_root_layout); 
layout.setOnBackPressedPreIMEListener(new OnBackPressedPreIMEListener() { 
    @Override 
    public void onBackPressedPreIME() { 
     InputMethodManager imm = (InputMethodManager)MyActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE); 
     View focusedView = MyActivity.this.getCurrentFocus(); 
     if(focusedView != null) 
      imm.hideSoftInputFromWindow(focusedView.getWindowToken(), 0); 
    } 
} 

顯然將您的實際活動的名稱替換MyActivity。這將允許你自己解除鍵盤而不是依靠系統爲你做。對於結果來說這是一項相對較大的工作量,但它是可靠的。

相關問題