我有一个带有 EditText 和按钮的活动。
当用户单击 EditText 时,会显示键盘,他可以输入一些文本 - 很好。
但是,当用户单击按钮时,我希望 EditText 不再成为焦点,即键盘隐藏,直到用户再次单击 EditText。
单击按钮后,我可以做什么来“隐藏 EditText 的焦点”。
我可以在按钮的 OnClick 方法中添加一些代码来做到这一点吗?
EDIT:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/edt_SearchDest"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:textSize="18sp"
android:hint="Enter your look-up here.." />
<Button
android:id="@+id/btn_SearchDest"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Search" />
</LinearLayout>
此致
将其放入按钮侦听器中:
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
EDIT
如果没有,上述解决方案将破坏您的应用程序EditText
是重点。像这样修改你的代码:
将此方法添加到您的类中:
public static void hideSoftKeyboard (Activity activity, View view)
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
然后,在按钮侦听器中,调用如下方法:
hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of the class and v is the View parameter used in the button listener method onClick.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)