Android EditText与键盘的关系及实现键盘搜索

1.项目中,在EditText中输入内容占满大部分屏幕时,若此时光标移到内容末尾,此时EditText上方的标题栏被顶出屏幕,但要求是顶部标题栏不被顶出,开始是在清单文件Activity节点下配置android:windowSoftInputMode="adjustPan",经过测试该方法没有发生作用,配置成android:windowSoftInputMode="adjustResize",此问题不再复发,但在测试时发现,如果输入控件(EditText),被固定住高度时(如设置为300dp),键盘弹出时,会覆盖住EditText,如果此时有内容也会被覆盖,如果宽高使用match_parent//wrap_content/weight设置时,则EditText控件会给键盘让出空间,不会被覆盖掉,键盘模式详情介绍见:点击打开链接

2.需要设置app字体不随系统字体的大小变化,使用sp为单位设置字体大小时,会跟随系统的字体大小变化,使用dp为单位时,便不会随系统的字体大小变化

3.搜索功能,使用键盘按钮搜索功能,控件属性:

 <EditText
        android:id="@+id/myedtx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionSearch"
        android:singleLine="true" />
搜索功能代码:
   myedtx.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 功能
                    return true;
                }
                return false;
            }
        });
    }

 
 

 4.在监听EditText内容字数变化的时,字符挨个输入没有问题,但是使用复制粘贴功能对内容进行复制粘贴到EditText中,多重复执行几次会导致app崩溃(没有测试完全,有的手机不会, 
 
 
 
应该和版本有关),因此用try{}catch{}语句,捕捉此类错误,使app不致于崩溃
  public static void editInputLength(final EditText editText, final int len, final Context context, final TextView tv_num) {
        editText.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                temp = charSequence;
            }

            @Override
            public void afterTextChanged(Editable editable) {
                int num = editable.length();
                tv_num.setText(num + "/" + len);
                if (temp.length() > len) {//当输入字数超过限制
                    UIHelper.showToast(context, "字数已超出限制,不可在输入!", false);
                    try {
                        editable.delete(editText.getSelectionStart() - 1, editText.getSelectionEnd());
                        editText.setText(editable);
                    }catch (IndexOutOfBoundsException error){
                        Log.i("Utils","数字超出");
                    }
                }
            }
        });

        InputFilter[] filters = {new InputFilter.LengthFilter(len + 1)};
        editText.setFilters(filters);
    }


 

猜你喜欢

转载自blog.csdn.net/androidforwell/article/details/53814234