Android之EditText特殊小技巧

来源:互联网 发布:mysql端口的作用 编辑:程序博客网 时间:2024/06/07 02:52

一、横屏时,弹出软键盘不全屏

不知你是否注意到,当我们手机横屏,且使用Android自带的软键盘为EditText进行文本输入时,若不进行特殊的设置,该软键盘会占用整个界面,那么,如何让键盘只占用屏幕的一部分呢? 其实只需要改一个小小的属性即可!

[html] view plain copy
  1. <EditText   
  2.     android:id="@+id/text1"   
  3.     android:layout_width="150dip"   
  4.     android:layout_height="wrap_content"  
  5.     android:imeOptions="flagNoExtractUi"/>  


 

另外使用android:imeOptinos可对Android自带的软键盘进行一些界面上的设置:

android:imeOptions="flagNoExtractUi"  //使软键盘不全屏显示,只占用一部分屏幕
同时,这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions="actionNone"  //输入框右侧不带任何提示
android:imeOptions="actionGo"    //右下角按键内容为'开始'
android:imeOptions="actionSearch"  //右下角按键为放大镜图片,搜索
android:imeOptions="actionSend"    //右下角按键内容为'发送'
android:imeOptions="actionNext"   //右下角按键内容为'下一步'
android:imeOptions="actionDone"  //右下角按键内容为'完成'

 

同时,可能EditText添加相应的监听器,捕捉用户点击了软键盘右下角按钮的监听事件,以便进行处理。

[java] view plain copy
  1. editText.setOnEditorActionListener(new OnEditorActionListener() {  
  2.         @Override  
  3.         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  4.             Toast.makeText(MainActivity.this"text2", Toast.LENGTH_SHORT).show();  
  5.             return false;  
  6.         }  
  7.     });  


 

 二、修改光标颜色

在使用EditText的XML 文件中加入一个属性:

android:textCursorDrawable="@null"

android:textCursorDrawable   这个属性是用来控制光标颜色的,

"@null"   是作用是让光标颜色和text color一样

android:textCursorDrawable 的用法可以查看android sdk

public static final int TextView_textCursorDrawable

 

Reference to a drawable that will be drawn under the insertion cursor.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol textCursorDrawable.

Constant Value: 70 (0x00000046)
原创粉丝点击