Android 设置EditText光标显示与隐藏,颜色及粗细

(一)Android 设置EditText光标颜色及粗细 

在android的输入框里,如果要修改光标的颜色及粗细步骤如下两步即可搞定:

1.在资源文件drawable下新建一个光标控制color_cursor.xml
 
  

<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <size android:width="1dp" /> <solid android:color="#008000" /> </shape>

2.设置EditText:android:textCursorDrawable="@drawable/color_cursor"

(二)Android 设置EditText光标颜色及与文本颜色一致 

在Android3.2或者更高版本上面,可以按照如下的方式进行设置:

EditText有一个属性:android:textCursorDrawable,这个属性是用来控制光标颜色的
 android:textCursorDrawable="@null"
"@null"作用是让光标颜色和文本颜色保持一致

(三)Android 修改光标位置
Android 的 EditText 控件默认获取焦点的时候, 插入光标是在第一个位置的,如果EditText中设置了文本, 这个时候光标是在文本的最前面, 而不是文本的最后. 为了方便用户使用, 需要把光标移动到文本最后, 但是EditText 没有提供 setCursor 函数.  经查看其文档 发现 setSelection 函数可以完成该功能. 如下: 
Java代码  
  1. EditText et = ... 
  2. String text = "text"
  3. et.setText(text); 
  4. et.setSelection(text.length());

(四)Android 光标显示与隐藏

   android:cursorVisible="true"//显示

   android:cursorVisible="false"//隐藏
   注:默认显示

Java代码  
  1. EditText et = ... 
  2. String text = "text"
  3. et.setText(text); 
  4. et.setSelection(text.length());

猜你喜欢

转载自blog.csdn.net/qq_22118507/article/details/51636639
今日推荐