2017-02-20 47 views
2

我在Android中构建的UI窗体中出现问题。 在这种形式中,我有一些编辑文本,用户必须在其中触摸它们以打开对话框片段。在对话框片段中,用户可以设置一个值,然后在触摸的edittext上显示该值。问题如下:当用户关闭对话框片段并且触摸的edittext获得焦点时,如果用户按下后退按钮,onBackPressed()方法不会被调用。当我从对话框片段中回来并且编辑文本获得焦点时,onBackPressed不会调用

我必须澄清,打开对话框片段的edittext不会显示键盘,因为用户无法在其上写入。我不想使用文字浏览。

在这里,我向您展示布局的一部分:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     .... 

     <android.support.design.widget.TextInputLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/text_input_when" 
      android:layout_marginTop="30dp"> 

      <EditText 
       android:hint="@string/meeting_when" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/edit_text_when" 
       android:textSize="18sp" 
       android:inputType="text|date" 
       android:textIsSelectable="true" 
       android:focusable="true" 
       android:drawableLeft="@drawable/ic_black_18dp" 
       android:drawableStart="@drawable/ic_black_18dp" 
       android:drawablePadding="10dp" 
       android:onClick="onEditTextWhenClicked" 
       android:nextFocusForward="@+id/edit_text_time"/> 

     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/text_input_time" 
      android:layout_marginTop="30dp"> 

      <EditText 
       android:hint="@string/meeting_time" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/edit_text_time" 
       android:textSize="18sp" 
       android:inputType="time" 
       android:textIsSelectable="true" 
       android:nextFocusForward="@+id/edit_text_place" 
       android:focusable="true" 
       android:drawableLeft="@drawable/ic__black_18dp" 
       android:drawableStart="@drawable/ic__black_18dp" 
       android:drawablePadding="10dp" 
       android:onClick="onEditTextTimeClicked" /> 

     </android.support.design.widget.TextInputLayout> 
.... 
.... 
</LinearLayout> 

enter image description here

因此,举例来说,如果用户触摸“时,”编辑文本,日期选择器对话框打开:

enter image description here

当用户设置日期时,关闭对话框片段并在编辑文本上设置该值

enter image description here

而现在,如果用户按下返回键,这是行不通的。

在活动中我有

@Override 
public void onBackPressed(){ 

    if (!areAllFieldEmpty()) { 

     showAlertCloseDialog(); 
    }else 
     super.onBackPressed(); 

} 

但这些方法不被调用。 。 我不知道如何解决它。请帮帮我。如果您需要更多信息,请告诉我。谢谢。如果是返回键,返回和使用的情况下返回true,返回false否则

if (mLayout != null) { 
    mLayout.setFocusableInTouchMode(true); 
} 
mLayout.setOnKeyListener(new View.OnKeyListener() { 
    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      getActivity().onBackPressed(); 
      return true; 
     } 
     return false; 
    } 
}); 

:当硬件密钥在您看来按下

+0

不过'super.onBackPressed()'正在采取行动清除编辑文本的焦点。尝试'edittext.clearFocus(); super.onBackPressed()'。 – zed

+0

我用过,但这导致焦点转到另一个控件,而不是转到以下内容。这破坏了表单的流程。 – AlphaDeveloper

+0

看到这个:http://stackoverflow.com/questions/6117967/how-to-remove-focus-without-setting-focus-to-another-control – zed

回答

1

注册您的片段回调被调用。

如果你有背部和键盘overrideonKeyPreIme()extending your EditText的烦恼:

@Override 
public boolean onKeyPreIme(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_BACK) { 
     // User has pressed Back key. So hide the keyboard 
     InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); 
     mgr.hideSoftInputFromWindow(this.getWindowToken(), 0); 
     // Hide your view as you do it in your activity 
    } else if (keyCode == KeyEvent.KEYCODE_MENU) { 
     // Eat the event 
     return true; 
    } 
    return false; 
} 
+1

谢谢,你真的帮了我很多!唯一的是我必须在onKeyPreIme方法中添加一个过滤器以避免它执行两次(if(event.getAction()== KeyEvent.ACTION_DOWN)) – AlphaDeveloper

+0

这也是一个很好的时刻来添加标志来自定义您的自定义行为'EditText'。我发现这个问题在工作中,并且已经创建了一个不包括我的情况,所以我使用get/setters创建了另外三个标志:'isShowingKeyboard()','isConsumingEvent()',另一个禁用它以便可重用在其他情况下。 – albodelu

+0

例如,用'isConsumingEvent()'替换'return true',或者加入'isKeepingFocus()'和'setKeepingFocus()'方法,'if(!isKeepingFocus()){this.clearFocus();}''after隐藏键盘,但你不喜欢这个:)。谢谢,随时修复答案或为将来的读者添加一个完整的'CustomEditText'示例:) – albodelu

相关问题