我正在启动一个带有文本编辑的对话框。我希望键盘在我触摸文本编辑字段时弹出,当我点击返回时,我希望它消失。
好吧,我明白了
final EditText commentTextEdit = new EditText(context);
commentTextEdit.setFocusableInTouchMode(false);
commentTextEdit.setOnTouchListener(new EditText.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
commentTextEdit.setFocusableInTouchMode(true);
InputMethodManager inputManager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(commentTextEdit.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
return false;
}
});
commentTextEdit.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
String s = commentTextEdit.getText().toString();
Log.d("Dialogs", "BarOptionsDialog got input "+s);
if(s.contains("\n"))
Log.d("Dialogs", "BarOptionsDialog found new line");
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(commentTextEdit.getWindowToken(), 0);
return false;
}
});
commentTextEdit.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
innerlayout.addView(commentTextEdit);
唯一的问题是,我不想要字符串中的 \n 。当我点击返回时触发 setOnEditorActionListener onEditorAction 但是当我查看字符串时,那里没有 \n
然而,当 setOnEditorActionListener onEditorAction 完成时,\n 出现在我不想要的编辑文本中。
有人知道如何阻止吗?
谢谢