Android EditText的设置
2015-05-26 16:30
127 查看
1、输入法Enter键图标的设置:
软件盘的界面替换只有一个属性android:imeOptions,这个属性的可以取的值有normal,actionUnspecified,actionNone,actionGo,actionSearch,actionSend,actionNext,actionDone,例如当值为actionNext时enter键外观变成一个向下箭头,而值为actionDone时enter键外观则变成了“完成”两个字。
我们也可以重写enter的事件
软键盘的Enter键默认显示的是“完成”文本,通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:
actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.
actionNone没有动作,对应常量EditorInfo.IME_ACTION_NONE
actionGo去往,对应常量EditorInfo.IME_ACTION_GO
actionSearch搜索,对应常量EditorInfo.IME_ACTION_SEARCH
actionSend发送,对应常量EditorInfo.IME_ACTION_SEND
actionNext下一个,对应常量EditorInfo.IME_ACTION_NEXT
actionDone完成,对应常量EditorInfo.IME_ACTION_DONE
(EditorInfo.inputType&EditorInfo.TYPE_CLASS_MASK)可以是许多不同的值,包括:
TYPE_CLASS_NUMBER
TYPE_CLASS_DATETIME
TYPE_CLASS_PHONE
TYPE_CLASS_TEXT
2、事件捕捉处理:
可以通过setOnEditorActionListener设置事件处理。
3、editor密码隐藏,怎么写?
有2种方法处理:
代码方法:input.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
layout配置方法:android:inputType="textPassword"
4、activity加载完成后,edit输入框会自动弹出输入法,可以通过以下代码屏蔽:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
5、设置EditText始终不弹出软件键盘
例:EditTextedit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
6、让EditText失去焦点,使用EditText的clearFocus方法
例如:EditTextedit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
7、EditText默认不弹出软件键盘
在AndroidMainfest.xml中选择activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden
8、设置光标到指定位置
8、EditText相关属性
EditText继承关系:View-->TextView-->EditText。
EditText的属性很多,这里介绍几个:
android:layout_gravity="center_vertical"
设置控件显示的位置:默认top,这里居中显示,还有bottom
android:hint="请输入数字!"
设置显示在空间上的提示信息
android:numeric="integer"
设置只能输入整数,如果是小数则是:decimal
android:singleLine="true"
设置单行输入,一旦设置为true,则文字不会自动换行。
android:password="true"
设置只能输入密码
android:textColor="#ff8c00"
字体颜色
android:textStyle="bold"
字体,bold,italic,bolditalic
android:textSize="20dip"
大小
android:capitalize="characters"
以大写字母写
android:textAlign="center"
EditText没有这个属性,但TextView有,居中
android:textColorHighlight="#cccccc"
被选中文字的底色,默认为蓝色
android:textColorHint="#ffff00"
设置提示信息文字的颜色,默认为灰色
android:textScaleX="1.5"
控制字与字之间的间距
android:typeface="monospace"
字型,normal,sans,serif,monospace
android:background="@null"
空间背景,这里没有,指透明
android:layout_weight="1"
权重,控制控件之间的地位,在控制控件显示的大小时蛮有用的。
android:textAppearance="?android:attr/textAppearanceLargeInverse"
不要让昨天的沮丧让今天的梦想安然失色
软件盘的界面替换只有一个属性android:imeOptions,这个属性的可以取的值有normal,actionUnspecified,actionNone,actionGo,actionSearch,actionSend,actionNext,actionDone,例如当值为actionNext时enter键外观变成一个向下箭头,而值为actionDone时enter键外观则变成了“完成”两个字。
我们也可以重写enter的事件
软键盘的Enter键默认显示的是“完成”文本,通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:
actionUnspecified未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.
actionNone没有动作,对应常量EditorInfo.IME_ACTION_NONE
actionGo去往,对应常量EditorInfo.IME_ACTION_GO
actionSearch搜索,对应常量EditorInfo.IME_ACTION_SEARCH
actionSend发送,对应常量EditorInfo.IME_ACTION_SEND
actionNext下一个,对应常量EditorInfo.IME_ACTION_NEXT
actionDone完成,对应常量EditorInfo.IME_ACTION_DONE
(EditorInfo.inputType&EditorInfo.TYPE_CLASS_MASK)可以是许多不同的值,包括:
TYPE_CLASS_NUMBER
TYPE_CLASS_DATETIME
TYPE_CLASS_PHONE
TYPE_CLASS_TEXT
2、事件捕捉处理:
可以通过setOnEditorActionListener设置事件处理。
finalEditTextinput=newEditText(this); input.setSingleLine(true);//android:singleLine=”true” input.setImeOptions(EditorInfo.IME_ACTION_SEND); input.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD); input.setOnEditorActionListener(newTextView.OnEditorActionListener(){ publicbooleanonEditorAction(TextViewv,intactionId, KeyEventevent){ Log.d(TAG,""+actionId+","+event); if(actionId==EditorInfo.IME_ACTION_SEND ||(event!=null&&event.getKeyCode()==KeyEvent.KEYCODE_ENTER)){ //dosomething; returntrue; } returnfalse; } });
3、editor密码隐藏,怎么写?
有2种方法处理:
代码方法:input.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
layout配置方法:android:inputType="textPassword"
4、activity加载完成后,edit输入框会自动弹出输入法,可以通过以下代码屏蔽:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
5、设置EditText始终不弹出软件键盘
例:EditTextedit=(EditText)findViewById(R.id.edit);
edit.setInputType(InputType.TYPE_NULL);
6、让EditText失去焦点,使用EditText的clearFocus方法
例如:EditTextedit=(EditText)findViewById(R.id.edit);
edit.clearFocus();
7、EditText默认不弹出软件键盘
在AndroidMainfest.xml中选择activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden
<activityandroid:name=".Main" android:label="@string/app_name" android:windowSoftInputMode="adjustUnspecified|stateHidden" android:configChanges="orientation|keyboardHidden"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
8、设置光标到指定位置
EditTextet=(EditText)findViewById(R.id.etTest); et.setSelection(2);
//设置光标不显示,但不能设置光标颜色 et.setCursorVisible(false); //获得焦点时全选文本 et.setSelectAllOnFocus(true); et.requestFocus();//请求获取焦点 et.clearFocus();//清除焦点 使用EditText的setError提示
et.setError("邮箱");
自定义图标的setError提示
Drawabledr=getResources().getDrawable(R.drawable.ic_launcher); dr.setBounds(0,0,10,10);//必须设置大小,否则不显示 et.setError("有错误提示",dr);
et.setInputType(InputType.TYPE_CLASS_PHONE);//只能输入电话号码 et.setInputType(InputType.TYPE_CLASS_NUMBER);//只能输入数字 et.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);//只能输入邮箱地址 et.setInputType(InputType.TYPE_NULL);//禁止输入(不弹出输入法) XML实现案例
<EditTextandroid:id="@+id/etTest"android:inputType="number" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
8、EditText相关属性
EditText继承关系:View-->TextView-->EditText。
EditText的属性很多,这里介绍几个:
android:layout_gravity="center_vertical"
设置控件显示的位置:默认top,这里居中显示,还有bottom
android:hint="请输入数字!"
设置显示在空间上的提示信息
android:numeric="integer"
设置只能输入整数,如果是小数则是:decimal
android:singleLine="true"
设置单行输入,一旦设置为true,则文字不会自动换行。
android:password="true"
设置只能输入密码
android:textColor="#ff8c00"
字体颜色
android:textStyle="bold"
字体,bold,italic,bolditalic
android:textSize="20dip"
大小
android:capitalize="characters"
以大写字母写
android:textAlign="center"
EditText没有这个属性,但TextView有,居中
android:textColorHighlight="#cccccc"
被选中文字的底色,默认为蓝色
android:textColorHint="#ffff00"
设置提示信息文字的颜色,默认为灰色
android:textScaleX="1.5"
控制字与字之间的间距
android:typeface="monospace"
字型,normal,sans,serif,monospace
android:background="@null"
空间背景,这里没有,指透明
android:layout_weight="1"
权重,控制控件之间的地位,在控制控件显示的大小时蛮有用的。
android:textAppearance="?android:attr/textAppearanceLargeInverse"
不要让昨天的沮丧让今天的梦想安然失色
相关文章推荐
- Android 设置EditText不显示光标
- 如何设置圆角的EditText——Android移动开发
- Android EditText设置InputType属性时,隐藏系统软键盘失败
- 【Android-小技巧】EditText设置光标
- Android 设置EditText显示或隐藏密码
- Android EditText 设置密码可见性(查看密码按钮的实现)
- Android中EditText和AutoCompleteTextView设置文字选中颜色方法
- android_EditText设置可以编辑和不可编辑状态
- Android EditText设置只能输入数字和小数点
- android 设置字体颜色、EditText自动输入转换成大写字母的多种方式
- Android中EditText,Button等控件的设置
- android-代码设置editText最大长度
- Android EditText设置imeOptions没有效果的解决方法
- Android EditText的设置错误提示信息
- android 设置EditText只能输入大写字母,Fragment的软键盘输入模式
- Android--Android教程]EditText设置/隐藏光标位置、选中文本和获取/清除焦点
- Android EditText设置只能输入数字和小数点
- Android EditText设置其不让自动获取焦点
- Android 利用Sharp样式设置文本框EditText圆角形状
- [Android教程]EditText设置/隐藏光标位置、选中文本和获取/清除焦点