Android之EditText特殊小技巧

xiaoxiao2021-02-28  80

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

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

[html]  view plain  copy <EditText        android:id="@+id/text1"        android:layout_width="150dip"        android:layout_height="wrap_content"       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 editText.setOnEditorActionListener(new OnEditorActionListener() {           @Override           public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {               Toast.makeText(MainActivity.this"text2", Toast.LENGTH_SHORT).show();               return false;           }       });  

 

 二、修改光标颜色

在使用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)
转载请注明原文地址: https://www.6miu.com/read-78647.html

最新回复(0)