在Android中完成键盘上隐藏软键盘?
我正在用软键盘上的完成按钮挣扎。我无法获得软键盘完成按键来隐藏键盘。从另一个按钮,它完美的作品与在Android中完成键盘上隐藏软键盘?
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
但onKeyListener不起作用我想要的方式。当我点击editText时,软键盘显示出来,其内容从字符中清除。
感谢收听!
main.xml中:
<EditText
android:id="@+id/answer"
android:layout_gravity="center_horizontal" android:textSize="36px"
android:inputType="phone"
android:minWidth="60dp" android:maxWidth="60dp"
/>
Java文件:
private EditText editText;
//...
editText = (EditText)findViewById(R.id.answer);
editText.setOnClickListener(onKeyboard);
editText.setOnKeyListener(onSoftKeyboardDonePress);
//...
// method not working:
private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
{
// code to hide the soft keyboard
imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
}
return false;
}
};
private View.OnClickListener onKeyboard=new View.OnClickListener()
{
public void onClick(View v)
{
editText.setText("");
}
};
使用按钮(在相同的Java档案)工作方法:
private View.OnClickListener onDone=new View.OnClickListener()
{
public void onClick(View v)
{
//....
// code to hide the soft keyboard
imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
}
};
编辑:当我按下键号“9”时,键盘隐藏。这很奇怪。
使用Android:imeOptions = “actionDone”,这样的:
<EditText
...
android:imeOptions="actionDone" />
InputMethodManager inputManager = (InputMethodManager)
context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput(0, 0);
上下文就是你的活动。
感谢您的努力!我将if语句更改为if(event.getKeyCode()== KeyEvent.KEYCODE_ENTER),使其与xml属性android:inputType =“phone”一起工作。我将保存您的答案,直到下一个软键盘问题。 BR - – 2010-08-05 17:02:31
将if语句更改为if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)
使其使用xml属性android:inputType="phone"
。
你应该有)为EditText上看看setOnEditorActionListener(:
设置一个特殊的监听器当上了 文本视图执行操作时被调用。当按下回车键时,或者当提供给IME的动作被用户选择时,这将被调用。
使用下面的代码与android:imeOptions="actionDone"
它为我工作。
<EditText
android:id="@+id/et_switch_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name"
android:imeOptions="actionDone"
android:inputType="textPersonName" />
工程就像一个魅力! :) – 2015-12-10 13:38:36
不适合我,在所谓的Android编程上真的很奇怪。 – 2016-12-16 03:50:18
当editext处于滚动视图时,它会出现错误。 – 2017-06-16 14:07:41