如何隐藏键盘,在EditText中添加文本和移动光标
How to hide the keyboard, and add text to EditText and move the cursor
我有一个 EditText
和一个 Button
。当你按 EditText
时,我不想显示 keyboard
,而当你按 Button
时,我想在 EditText
上输入数字 1。
我要cursor
的观察没有消失。
@Karim Michel. 你可以使用
第一种情况.隐藏键盘
ETOBJ.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
InputMethodManager inputManager = (InputMethodManager) context.
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
return false;
}
});
当按下按钮时,您可以使用
setCursorVisible(false);
setText("1");
试试这些代码:
your_edit_text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
your_edit_text.setInputType(InputType.TYPE_NULL);
EditText.setText("1");
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
your_edit_text.setSelection(your_edit_text.getText().length()); // move cursor to end
}
我有一个 EditText
和一个 Button
。当你按 EditText
时,我不想显示 keyboard
,而当你按 Button
时,我想在 EditText
上输入数字 1。
我要cursor
的观察没有消失。
@Karim Michel. 你可以使用
第一种情况.隐藏键盘
ETOBJ.setOnTouchListener(new View.OnTouchListener()
{
public boolean onTouch(View arg0, MotionEvent arg1)
{
InputMethodManager inputManager = (InputMethodManager) context.
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(
this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
return false;
}
});
当按下按钮时,您可以使用
setCursorVisible(false);
setText("1");
试试这些代码:
your_edit_text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
your_edit_text.setInputType(InputType.TYPE_NULL);
EditText.setText("1");
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
your_edit_text.setSelection(your_edit_text.getText().length()); // move cursor to end
}