隐藏返回键上的软键盘按
我搜索了半打其他答案,但没有找到一个有效。我所要做的就是在用户按下输入按钮时关闭软键盘。 (相当于非常简单的iOS'resignKeyboard'调用。)在下面的代码中,onEditorAction方法不会被调用。我已经成立了一个EditText视图在我的XML文件,并在我的片段中的代码如下:隐藏返回键上的软键盘按
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragView = inflater.inflate(R.layout.fragment_encrypt2, container, false);
textField = (EditText) fragView.findViewById(R.id.textField);
textField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView arg0, int actionId,
KeyEvent arg2) {
// hide the keyboard and search the web when the enter key
// button is pressed
if (actionId == EditorInfo.IME_ACTION_GO
|| actionId == EditorInfo.IME_ACTION_DONE
|| actionId == EditorInfo.IME_ACTION_NEXT
|| actionId == EditorInfo.IME_ACTION_SEND
|| actionId == EditorInfo.IME_ACTION_SEARCH
|| (arg2.getAction() == KeyEvent.KEYCODE_ENTER)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(textField.getWindowToken(), 0);
return true;
}
return false;
}
});
// Inflate the layout for this fragment
return fragView;//inflater.inflate(R.layout.fragment_encrypt2, container, false);
}
以下是从我定义的EditText字段的XML文件中的一个片段。我需要EditText为多行。
<EditText
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="@string/Encrypted_Text_Hint"
android:gravity="top"
android:inputType="textMultiLine"
android:imeOptions="actionDone"/>
,你也可以把imeOptions为完成这样
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
我克利说不上wheather你正在努力实现这个或not.Any方式看一看。
编辑: android:singleLine由于性能不佳而不推荐使用API 3,您必须改用android:maxLines。 singleLine将可用,因为即使现在一些效果不支持android:maxLines属性。
这可能是你可以一个属性添加到您的EditText是这样的:如果你不想多行,你EDITTEXT你可以指定单行的EditText上
android:imeOptions="actionSearch"
即使有这个选项,仍然ActionListener的不叫 – cph2117 2014-10-30 15:15:28
试试这个方法,它可能会解决你的问题。
protected void showKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() == null) {
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
} else {
View view = activity.getCurrentFocus();
inputMethodManager.showSoftInput(view,InputMethodManager.SHOW_FORCED);
}
}
/**
* Hide keyboard.
*/
protected void hideKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = activity.getCurrentFocus();
if (view == null) {
if (inputMethodManager.isAcceptingText())
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
} else {
if (view instanceof EditText)
((EditText) view).setText(((EditText) view).getText().toString()); // reset edit text bug on some keyboards bug
inputMethodManager.hideSoftInputFromInputMethod(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
问题是actionListener没有被调用,所以我甚至无法调用这些方法 – cph2117 2014-10-30 15:12:06
我通过改变解决了这个问题从XML文件中的以下内容:
android:inputType="textMultiLine"
到:
android:inputType="textImeMultiLine"
Close!这会将焦点移至布局中的下一个项目,但不会关闭软键盘。 – 2016-08-28 01:09:19
下面的代码为我工作。
以XML:
android:imeOptions="actionDone"
android:inputType="textCapWords"
IN JAVA CLASS:
edit_text.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// TODO Auto-generated method stub
if ((actionId==EditorInfo.IME_ACTION_DONE) )
{
//Toast.makeText(getActivity(), "call",45).show();
// hide virtual keyboard
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(auto_data.getWindowToken(), 0);
return true;
}
return false;
}
});
EditText.xml
<EditText
android:id="@+id/edit_text_searh_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:singleLine="true"
android:lines="1"
android:hint="Ingresa palabras claves"
android:imeActionLabel="Search"
android:background="@drawable/rounded_edit_text_search"
android:drawableRight="@android:drawable/ic_menu_search"/>
Fragment.java
final EditText edit_text_searh = (EditText) v.findViewById(R.id.edit_text_searh_home);
edit_text_searh.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit_text_searh.getWindowToken(), 0);
Toast.makeText(getContext(),edit_text_searh.getText(),Toast.LENGTH_SHORT).show();
return true;
}
});
接受的答案被弃用:
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
尝试这样的:
<EditText
android:id="@+id/edittext_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:maxLines="1"
/>
根据我的测试,什么是真正需要解雇的软键盘,当用户点击回车键,就可以实现只需将下面的代码添加到xml中的EditText中即可。
android:imeOptions="actionDone"
android:inputType="textImeMultiLine"
不需要OnEditorActionListener或任何其他Java代码。
您尚未在EditText上设置任何'imeOptions',在这种情况下返回被认为是另一个输入字符。 – 2014-10-30 04:50:02
我已经设置imeOptions,但我需要的EditText是多 – cph2117 2014-10-30 15:09:56