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

来源:互联网 发布:mac 局域网无法ping通 编辑:程序博客网 时间:2024/05/05 18:37

(原创)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;              }          });  

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




1 0