关于EditText

1、控件EditText的setOnEditorActionListener方法的使用

setOnEditorActionListener这个方法,并不是在我们点击EditText的时候触发,也不是在我们对EditText进行编辑时触发,而是在我们编辑完之后点击软键盘上的各种键才会触发。

通过 布局文件 中的imeOptions可以控制软件盘右下角的按钮显示为不同按钮。所以和EditorInfo搭配起来可以实现各种软键盘的功能。
imeOptions=”actionUnspecified” –> EditorInfo.IME_ACTION_UNSPECIFIED
imeOptions=”actionNone” –> EditorInfo.IME_ACTION_NONE
imeOptions=”actionGo” –> EditorInfo.IME_ACTION_GO
imeOptions=”actionSearch” –> EditorInfo.IME_ACTION_SEARCH
imeOptions=”actionSend” –> EditorInfo.IME_ACTION_SEND
imeOptions=”actionNext” –> EditorInfo.IME_ACTION_NEXT
imeOptions=”actionDone” –> EditorInfo.IME_ACTION_DONE

布局中定义一个EditText控件

 <EditText
        android:id="@+id/ET_phonenumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/buttonAdd"
        android:hint="@string/enter_new_note" 
        android:imeOptions="actionDone"// 这里和onEditorAction中actionId对应。
        android:inputType="text"/>

添加setOnEditorActionListener方

ET_phonenumber.setOnEditorActionListener(new OnEditorActionListener() {  
            @Override  
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
               if (actionId == EditorInfo.IME_ACTION_DONE) {   
                // 按下完成按钮,这里和上面imeOptions对应
                text.setText("Editing EditorInfo.IME_ACTION_DONE");  
                return false;   //返回true,保留软键盘。false,隐藏软键盘
                }
            }  
        });  

如果发现 setOnEditorActionListener方法不会被调用,尝试更换输入法,AOSP输入法貌似不支持


2、 android 软件盘事件响应:android: imeOptions 、KeyEvent、android: inputType

android 软件盘事件响应:
在android中,有时需要对EditText实现软件盘监听的场景。当android按下软键盘的时候,响应完成、发送、搜索或者其他事件。
Google 提供了 EditorInfo、KeyEvent 的一些方法,能够实现我们需要的功能。详细可研究:EditorInfo.class 和 KeyEvent.class.

2.1 EditorInfo

把EditText的ImeOptions属性设置成不同的值,Enter键上可以显示不同的文字或图案。
actionNone :回车键,按下后光标到下一行
actionGo : Go
actionSearch : 一个放大镜
actionSend : Send
actionNext : Next
actionDone : Done,隐藏软键盘,即使不是最后一个文本输入框

监听方法:

private final EditText.OnEditorActionListener editorActionListener =
           new TextView.OnEditorActionListener() {
               @Override
               public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                   if (actionId == KeyEvent.ACTION_DOWN || actionId == EditorInfo.IME_ACTION_DONE) {
                       //业务代码
                       haoMent.createTest(Test.getId(), v.getText().toString());
                       UiUtils.hideSoftKeyboard(getApplicationContext(), haoTest.this);
                       v.setText("");
                       v.clearFocus();
                       handler.post(updateView);
                   }
                   return true;
               }
          };
2.2 KeyEvent

如果手机的输入法不是内置输入法,而是其他第三方输入法,那么可能会发生软件盘回车键无响应的问题。为了防止该类事情,响应的其KeyEvent。

在代码中添加事件响应:

 
inputKey = (EditText) findViewById(R.id.contactSearch_editText);
inputKey.addTextChangedListener(watcher);
 
inputKey.setOnKeyListener(new View.OnKeyListener() {
@Override
  public boolean onKey(View v, int keyCode, KeyEvent event) {
    
 
  if (KeyEvent.KEYCODE_ENTER == keyCode && event.getAction() == KeyEvent.ACTION_DOWN) {
    handler.post(updateView);
    return true;
  }
  return false;
  }
});
//响应键盘内容
public TextWatcher watcher = new TextWatcher() {
 
  @Override
  public void beforeTextChanged(CharSequence charSequence, int i, int i2,int i3) {
 
  }
 
  @Override
  public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
 
  }
 
  @Override
  public void afterTextChanged(Editable editable) {
 
  handler.post(updateView);
 
  }
};
2.3 android 输入类型:android:inputType
密码框属性 android:password="true"   让EditText显示的内容自动为星号,输入时内容会在1秒内变成*字样。

纯数字 android:numeric="true"      让输入法自动变为数字输入键盘,同时仅允许0-9的数字输入

仅允许 android:capitalize="haoTest"   仅允许接受输入haoTest,一般用于密码验证

android:editable="false"         设置EditText不可编辑

android:singleLine="true"        强制输入的内容在单行

android:ellipsize="end"         自动隐藏尾部溢出数据,一般用于文字内容过长一行无法全部显示时
2.4关于EditText 设置不换行无效 

xml里面设置了

android:maxLines="1"
android:lines="1"
然后发现还是可以换行 ,加上 android:singleLine="true" 后,只显示一行,不再进行换行
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaowang_lj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值