(原创)EditText软键盘弹出关闭等使用总结

(原创)EditText软键盘弹出关闭等使用总结

1.关于EditText常用属性设置:

(1).去除其默认划线背景方式:设置背景色透明即可
	
android:background="#00000000"
(2).设置其显示闪烁光标方式:光标可见
android:cursorVisible="true"
(3).设置单行:
android:singleLine="true"
(4).设置提醒文本颜色:
android:textColorHint="#dddddd"
(5).去除光标色:
android:textCursorDrawable="@null"

基本就这些设置了,关于弹出软键盘属性设置应该设置到清单文件相应的activity里面,因为其影响的是整个activity。

2.设置一开始进来不弹出软键盘并且软键盘弹出后不会重新绘制activity里面的view(避免收缩软键盘时activity底部出现一段空白):

 <activity
            android:name="对应的activity"
            android:windowSoftInputMode="adjustNothing|stateHidden" />//其中adjustNothing设置不影响activity重新绘制view布局,stateHidden第一次进来隐藏软键盘

3.代码动态设置弹出软键盘和关闭软键盘方式:

(1).代码动态弹出软键盘方式:
<span style="white-space:pre">	</span>editText.setCursorVisible(true);//动态代码设置显示光标方式
 <span style="white-space:pre">	</span>//代码动态弹出软键盘
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
(2).代码动态关闭弹出的软键盘方式:
 <span style="white-space:pre">	</span>editText.setCursorVisible(false);//动态代码设置隐藏guangbiao
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

4.关于EditText一些常用的监听事件的使用:

(1).关于其输入text文本的完成或者发生变化的监听:addTextChangedListener()监听输入框变化状态
editText.addTextChangedListener(new TextWatcher() {  
            @Override  
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {  
        //输入变化前执行  
            }  
  
            @Override  
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {  
        //输入文本发生变化执行  
            }  
  
            @Override  
            public void afterTextChanged(Editable editable) {  
        //输入文本停止后的执行方法  
            }  
        });  
(2).关于其软键盘里面各个控件操作行为的监听:
 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
            @Override  
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
                if (actionId == EditorInfo.IME_ACTION_DONE) {//点击软键盘完成控件时触发的行为  
            //关闭光标并且关闭软键盘  
                    editeText.setCursorVisible(false);  
                    InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
                    im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
                }  
                return true;//消费掉该行为  
            }  
        });  
(3).关于其获取到焦点事件的监听: 
edtiText.setOnTouchListener(new View.OnTouchListener() {  
            @Override  
            public boolean onTouch(View view, MotionEvent motionEvent) {  
        //获取到焦点显示光标  
                editText.setCursorVisible(true);  
                return false;  
            }  
        });  

以上暂时个人开发过程中使用到的一些知识点整理。




  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值