隐藏键盘Android的最佳方法 [英] Best way to hide keyboard in Android

查看:92
本文介绍了隐藏键盘Android的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道输入文字的EditText后隐藏键盘的最佳途径。

I would like to know the best way to hide keyboard after entering the text to EditText.

1)setonfocuschangelistener:这是否监听器只发射,当完成按钮是pressed或当焦点的变化从一个EditText上其他?当我用这个方法,我不能隐藏键盘。

1) setonfocuschangelistener : Does this listener is fired only, when the done button is pressed or when the focus changes from one EditText to other? When I used this method, I couldn't hide the keyboard.

2)setOnTouchListener:当我用这个,我可以隐藏键盘,但我怀疑有可能是一个问题与此有关。在这种情况下,我加入了触摸监听到根的LinearLayout。继code我用了:

2) setOnTouchListener : When I used this, I could hide the keyboard, but i doubt there might be an issue with this. In this case, I add the touch listener to the root LinearLayout. Following code I had used:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    txtUserName = (EditText)findViewById(R.id.txtUserName);
    btnLogin = (Button)findViewById(R.id.btnLogin);
    layoutView = (LinearLayout)findViewById(R.id.li);

    layoutView.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(txtUserName
                    .getWindowToken(), 0);
            return true;
        }
    });
}

里面的主要的LinearLayout,我使用的另外两个LinearLayouts。我面对上面的code的问题是,在我pressed一些分数,键盘不会隐藏。 我的疑问是,我加入了触摸监听器只与根布局,不给触摸监听器与其他内部布局或其他控件(TextView中)。当我接触过其他控件或周围的TextView(即,内部布局)的一些点,键盘不会隐藏。

Inside the main LinearLayout, I am using other two LinearLayouts. The issue that i faced with the above code is that at some points when I pressed, the keyboard doesn't hides. My doubt is that I am adding touch listener only with root layout, not giving touch listener with other inner layouts or other controls(TextView). When I touch over other controls or some points around the TextView(ie, inner layouts), keyboard doesn't hides.

这意味着我需要touchListener添加到根布局内的所有版面或控制? 如何这种情况能够以更好的方式来处理?

That means do i need to add touchListener to all layouts or controls inside the root layout? How this situation can be handled in a better way?

推荐答案

您可以使用此code

You can use this code

InputMethodManager imm = 
    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditView.getWindowToken(), 0);

这篇关于隐藏键盘Android的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆