安卓EditText使用总结

本文是在日常开发中所遇到的EditText使用需求的总结。

一、光标移动到文本框末尾。

if (!TextUtils.isEmpty(editText.getText())) {
    editText.setSelection(editText.getText().length());
}

二、实现搜索功能。

<EditText
    android:id="@+id/edit_text"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#ffffff"
    android:hint="请输入要搜索的关键字"
    android:imeOptions="actionSearch"
    android:paddingLeft="10dp"
    android:singleLine="true"
    android:textColor="#000000"
    android:textColorHint="#989898"
    android:textSize="13sp" />
 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
     @Override
     public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
         if (actionId == EditorInfo.IME_ACTION_SEARCH) {
             // 当按了搜索之后关闭软键盘
             ((InputMethodManager) editText.getContext().getSystemService(
                            Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                            MainActivity.this.getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
             return true;
          }
          return false;
     }
 });

猜你喜欢

转载自blog.csdn.net/youngmy5/article/details/82627338