我正在启动一个带有文本编辑功能的对话框。我希望当我触摸文本编辑字段时弹出键盘,当我点击返回时,我想让它消失。
我在这里得到了
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出现在我不想要的编辑文本中。
任何人都知道如何阻止它?
感谢