onclick事件显示和隐藏软键盘,和edittext获取焦点

来源:互联网 发布:淘宝能查到快递重量吗 编辑:程序博客网 时间:2024/05/17 19:22
在onclick事件下.以下方法可行.(如果是EditText失去焦点/得到焦点,没有效果) 

隐藏:

private void hideKeyboard(){InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);}

显示:

我的用意是点击按钮后显示edittext,而且自动获取焦点并弹出软键盘。

search_layout.setVisibility(View.GONE);share_top2.setVisibility(View.VISIBLE);

showKeyboard的代码:

private void showKeyboard(){    InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);     im.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);    }


edittext获取焦点:

search_edit.requestFocus();

这样的话可以每次显示edittext后自动焦点在edittext上。


0 0