安卓EDITTEXT点击一个按钮后,移除焦点 [英] android edittext remove focus after clicking a button
问题描述
我有一个EditText和一个按钮的活动。 当用户点击的EditText,键盘显示,他可以输入一些文字 - 罚款。 但是,当用户点击按钮我想要的EditText是没有更为清晰,即键盘隐藏直到用户再次点击了的EditText。
I have an Activity with an EditText and a Button. When the User clicks on the EditText, the keyboard is shown and he can type in some Text - fine. But when the user clicks on the Button I want the EditText to be no more in focus i.e. the keyboard hides til the user clicks again on the EditText.
我能做些什么来隐藏对焦中的EditText中,按钮被点击后。 有些code,我可以在onclick方法添加一个按钮来做到这一点?
What can I do to 'hide the focus' of the EditText, after the Button is clicked. Some Code I can add in the OnClick Method of the Button to do that?
编辑:
<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>
最好的问候
推荐答案
在您的按钮,监听器将这个:
Put this in your button listener:
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
修改
上述解决方案将打破你的应用程序,如果没有的EditText
的重点是。修改您的code是这样的:
The solution above will break your app if no EditText
is focused on. Modify your code like this:
这个方法添加到您的类:
add this method to you class:
public static void hideSoftKeyboard (Activity activity, View view)
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
然后,在你的按钮监听器,调用该方法是这样的:
Then, in your button listener, call the method like this:
hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of the class and v is the View parameter used in the button listener method onClick.
这篇关于安卓EDITTEXT点击一个按钮后,移除焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!