这里我有一个片段,它包含一个编辑文本,我想在 Enter Key Press 上隐藏软键盘。
我试过这个。但似乎我的 onKey 方法没有被调用。我已经检查过了Log
edTxtUserText.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
edTxtUserText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edTxtUserText.getWindowToken(),
0);
return true;
}
return false;
}
});
- 我的代码有什么问题。
这也是我的editText XML
<EditText
android:id="@+id/edTxtPreview"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/txtText"
android:layout_marginRight="24dp"
android:background="@drawable/edit_text_style"
android:ems="10"
android:imeOptions="flagNoExtractUi"
android:inputType="textCapWords|text"
android:lines="1"
android:selectAllOnFocus="true"
android:text="Your Text Here"
android:textColor="@android:color/white"
android:textSize="18sp" >
</EditText>
我也在这个editText上设置了OnClickListner。作为
edTxtUserText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
edTxtUserText.requestFocus();
edTxtUserText.selectAll();
((InputMethodManager) getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE)).toggleSoftInput(
InputMethodManager.SHOW_FORCED,
InputMethodManager.HIDE_IMPLICIT_ONLY);
}
});