Android:响应软键盘搜索

博客介绍了Android开发中的XML配置,如设置android:imeOptions和android:singleLine,还提及了使用或隐藏软键盘的相关内容,主要围绕Android开发的基础操作展开。

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

1、xml配置

  android:imeOptions="actionSearch"
   android:singleLine="true"

<EditText
    android:id="@+id/edt_search"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@color/white"
    android:hint="搜索姓名/工号"
    android:imeOptions="actionSearch"
    android:singleLine="true"
    android:textColor="@color/color_333"
    android:textSize="14sp" />

2、使用 

private void initSearch() {
        //搜索
        mEdtSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    // 当按了搜索之后关闭软键盘
                    ((InputMethodManager) mEdtSearch.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            AddTaskActivity.this.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
                    mSearchKey = mEdtSearch.getText().toString().trim();
                    getPersonListData();
                 
                }

                return false;
            }
        });
    }

或者

       //搜索
        editText.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                mIvSearch.performClick(); //某个按钮点击事件
              
            }
            return false;
        });

隐藏软键盘

/**
     * 隐藏键盘
     */
    protected void hideInput(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值