前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android入门教程 | EditText 用户输入

Android入门教程 | EditText 用户输入

原创
作者头像
Android_anzi
修改2021-11-08 09:14:58
7800
修改2021-11-08 09:14:58
举报

EditText 监听回车

使用EditText时,有时候我们会需要监听输入的回车,以做出一些操作。 或者需要把回车变成“搜索”,“发送”或“完成”等等。

EditText 为我们提供了一个属性 imeOptions 用来替换软键盘中 enter 键的外观,如actionGo 会使外观变成“前往”。 需要同时设置 android:inputType="text"

代码语言:txt
复制
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionGo"
    android:inputType="text" />

常用的几个属性以及替换的文本外观:

属性

说明

对应静态变量

actionUnspecified

未指定

EditorInfo.IME_ACTION_UNSPECIFIED

actionNone

动作

EditorInfo.IME_ACTION_NONE

actionGo

去往

EditorInfo.IME_ACTION_GO

actionSearch

搜索

EditorInfo.IME_ACTION_SEARCH

actionSend

发送

EditorInfo.IME_ACTION_SEND

actionNext

下一项

EditorInfo.IME_ACTION_NEXT

actionDone

完成

EditorInfo.IME_ACTION_DONE

设置的方法可以在布局文件中设置 android:imeOptions="actionNext" 或者在代码中 mUserEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);

接下来设置回车按键的监听事件 setOnEditorActionListener

代码语言:txt
复制
et1.setOnEditorActionListener(mOnEditorActionListener);
    // ......
    private TextView.OnEditorActionListener mOnEditorActionListener = new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            Log.d(TAG, "actionId: " + actionId);
            Log.d(TAG, "event:    " + event);
            return false;
        }
    };

如果 onEditorAction 返回 true,表示消费调这个事件。

上面的 actionId 对应的是 android.view.inputmethod.EditorInfo 中的常量。

代码语言:txt
复制
public static final int IME_ACTION_UNSPECIFIED = 0x00000000;
public static final int IME_ACTION_NONE = 0x00000001;
public static final int IME_ACTION_GO = 0x00000002;
public static final int IME_ACTION_SEARCH = 0x00000003;
public static final int IME_ACTION_SEND = 0x00000004;
public static final int IME_ACTION_NEXT = 0x00000005;
public static final int IME_ACTION_DONE = 0x00000006;
public static final int IME_ACTION_PREVIOUS = 0x00000007;

EditText 光标移动与选择

主要介绍 setSelection 方法。

setSelection 有:

  • setSelection(int start, int stop) 选择范围
  • setSelection(int index) 把光标移动到指定位置

例:假设有EditText,变量名为mEt1

  • 把光标移动到最前mEt1.setSelection(0);mEt1.setSelection(mEt1.getText().length());mEt1.setSelection(mEt1.getSelectionEnd() + 1);mEt1.setSelection(mEt1.getSelectionEnd() - 1);要注意的是,如果传入的index超出了text的范围,会报 java.lang.IndexOutOfBoundsException 因此在实际工程中,需要判断传入的位置是否在EditText已有内容的长度范围内。
  • 把光标移动到最后
  • 光标右移一位
  • 光标左移一位
  • 全选当前输入的textmEt1.setSelection(0, mEt1.getText().length());

监听输入内容

代码中动态限制输入长度

使用TextWatcher

代码语言:txt
复制
mQueryEt.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        // 如果EditText中的数据不为空,且长度大于指定的最大长度
        if (!TextUtils.isEmpty(s) && s.length() > 15) {
            // 删除指定长度之后的数据
            s.delete(15, s.length() - 1);
        }
    }
});

Android零基础入门教程视频参考

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • EditText 监听回车
  • EditText 光标移动与选择
  • 监听输入内容
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档