Android跟软键盘的故事

1 软键盘弹出 EditText向上顶

方法一:只需要在 Androidmanifest中在对应的activity中添加

 android:windowSoftInputMode="adjustResize|stateHidden"

方法二:在Activity中的oncreate中setContentView之前写上这个代码

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

监控软键盘确定 搜索 按钮并赋予点击事件

在android的实践开发中,为了界面的美观,往往那些搜索框并没有带搜索按钮,而是调用了软键盘的搜索按钮

首先在xml配置文件中添加属性属性: Android:imeOptions=”actionSearch”,这样我们调用软键盘时,回车键就会显示搜索二字。 
EditText et_search= (EditText) findViewById(R.id.search_et);
watchSearch();

//监控软件盘的搜索按钮
public void watchSearch() {
    et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { // 先隐藏键盘 ((InputMethodManager) et_search.getContext() .getSystemService(Context.INPUT_METHOD_SERVICE)) .hideSoftInputFromWindow(SearchActivity.this .getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); /*
				执行具体操作
				*/
                return true;
            }
            return false;
        }
    });
}

3 输入之后点击按钮 软键盘消失

InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext().
        getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(et_address.getWindowToken(), 0);
//et_address为按钮上面的一个EditText


想要实现该功能只需要重写 public boolean onTouchEvent(MotionEvent event)方法

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
@Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (MakeActivity.this.getCurrentFocus() != null) { if (MakeActivity.this.getCurrentFocus().getWindowToken() != null) { imm.hideSoftInputFromWindow(MakeActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } } return super.onTouchEvent(event); }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘟嘟嘟~~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值