隐藏软键盘

本文详细介绍了一个包含输入提示、麦克风图标及软键盘管理的搜索框组件的UI设计与交互逻辑实现。文章深入探讨了如何通过代码设置搜索框的属性,如背景颜色、边距、填充等,并实现了焦点变化监听及软键盘自动隐藏功能。此外,还介绍了如何响应搜索操作,包括IME_ACTION_SEARCH的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<EditText
    android:id="@+id/ed_search"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:layout_weight="1"
    android:background="@color/background_gray"
    android:drawableLeft="@mipmap/ic_search"
    android:drawableRight="@mipmap/ic_mic"
    android:drawablePadding="6dp"
    android:hint="请输入网点名称"
    android:imeActionId="84"
    android:imeActionLabel="search"
    android:imeOptions="actionSearch"
    android:maxLines="1"
    android:paddingLeft="6dp"
    android:paddingRight="6dp"
    android:singleLine="true"
    android:textSize="14sp" />
ed_search_input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean b) {
        if (!b) {
            hideSoftInput();
        }
    }
});

 

ed_search_input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if (i == EditorInfo.IME_ACTION_SEARCH) {
            queryMark(ed_search_input.getText().toString().trim());
            return true;
        }
        return false;
    }
});

 

private void hideSoftInput() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm != null)
        imm.hideSoftInputFromInputMethod(ed_search_input.getWindowToken(), 0);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值