2013-03-21 52 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。这将允许你自己解除键盘而不是依靠系统为你做。对于结果来说这是一项相对较大的工作量,但它是可靠的。

相关问题