学习android也有好长一段时间了,但是在用的过程中却总是还要上网查资料。今天再次回来学习EditText,在此对其属性做一个简单的总结。
1、新建一个工程"EditTextDemo",目录如下:
2、EditText属性详解
1:最基本属性layout_width、layout_height指定EditText宽度和高度,其xml内容如下:
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"></EditText>
2:捕捉软件盘“enter”键按下时,让下一个EditView获取焦点,其xml内容如下:
<EditText android:id="@+id/goNextAvailableEditText"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:imeOptions="actionNext">" </EditText>
3:横屏状态下,给EditText留出空间,而不是铺满屏幕,其xml内容如下:
<EditText android:id="@+id/notFullScreen"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:imeOptions="flagNoExtractUi">" </EditText>
4:EditText 的“numeric”属性,取值有“signed”,“integer”,“decimal”,分别为带符号整数,整数,浮点数,这里以浮点数为例,其xml内容如下:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:numeric="decimal" ></EditText>
5:限定只能输入电话号码,其xml内容如下:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:phoneNumber="true" ></EditText>
6:限定输入为密码,其xml内容如下:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:password="true" > </EditText>
7:限定用户输入长度,当用户输入超过限定数量时将被丢弃,其xml内容如下:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:maxLength="6" ></EditText>
8:限定EditText为单行,其xml内容如下:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:singleLine="true" ><!-- 默认时我的值为false --> </EditText>
9:不影响用户输入,仅想给用户一个提示时,可以这样做
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:hint="用户名..." android:textColorHint="#e8002378"><!-- “用户名...”自定义颜色 --> </EditText>
10:不可编辑EditText相当于一个TextView,其xml内容如下:
<EditText android:id="@+id/uneditable" android:layout_width="match_parent"
android:layout_height="wrap_content" android:enabled="false"
android:text="遇到这时的我,换用TextView或许是更好的选择" > </EditText>
11:EditText的“InputType”属性指定了软件盘的输入类型,这里以date为例,其xml内容如下:
<EditText android:id="@+id/date" android:layout_width="match_parent"
android:layout_height="wrap_content" android:inputType="date" />
12:EditText的imeOptions指定了软键盘enter键的显示内容,这里以actionDone为例:
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:imeOptions="actionDone"> </EditText>
- 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 效果:
3、main.xml内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<!-- 牢记了,“ellipsize” 属性可不适用于EditText哦 ——设置了不会出错,但是不会有效果。-->
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="100dp" android:layout_height="wrap_content"
android:ellipsize="middle" android:text="这是我最简单的形态" android:singleLine="true" />
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" > </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="点击enter进入下一个EditText"/> <EditText
android:id="@+id/goNextAvailableEditText" android:layout_width="match_parent"
android:layout_height="wrap_content" android:imeOptions="actionNext">"
</EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="横屏时软键盘会给我留下空间哦"/>
<EditText android:id="@+id/notFullScreen" android:layout_width="match_parent"
android:layout_height="wrap_content" android:imeOptions="flagNoExtractUi">"
</EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="这里只能输入整数:"/>
<EditText android:id="@+id/editText1" android:layout_width="match_parent"
android:layout_height="wrap_content" android:numeric="integer" > </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="这里只能输入带符号的整数:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:numeric="signed" >
</EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="这里只能输入浮点数:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:numeric="decimal" > </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="这里只能输入电话号码:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:phoneNumber="true" >
</EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" ndroid:layout_height="wrap_content"
android:text="这里输入的是密码:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:password="true" > </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="这里限定了用户输入的长度6:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:maxLength="6" > </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="这里限定了为单行:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:singleLine="true" ><!-- 默认时我的值为false --> </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="我不会影响你的输入哦:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:hint="随便输..." android:textColorHint="#e8002378"><!-- “随便输...”自定义颜色 --> </EditText>
</LinearLayout>
<Button android:id="@+id/goNext" android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="我要继续验证..."/>
</LinearLayout>
4、second.xml内容如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="不能编辑我了哦:"/>
<EditText android:id="@+id/uneditable" android:layout_width="match_parent"
android:layout_height="wrap_content" android:enabled="false"
android:text="遇到这时的我,换用TextView或许是更好的选择" > </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="指定输入软键盘类型(date):"/><!-- android:inputType="date" -->
<EditText android:id="@+id/date" android:layout_width="match_parent"
android:layout_height="wrap_content" android:inputType="date">
</EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="IME Options=actionGo:"/>
<EditText android:id="@+id/actionGoet" android:layout_width="match_parent"
android:layout_height="wrap_content" ><!-- android:imeOptions="actionGo" -->" </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="IME Options=actionSearch:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:imeOptions="actionSearch"> </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="IME Options=actionSend:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:imeOptions="actionSend">
</EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="IME Options=actionDone:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:imeOptions="actionDone"> </EditText>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="IME Options=flagNavigatePrevious:"/>
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:imeOptions="flagNavigatePrevious">" </EditText>
</LinearLayout>
</LinearLayout>
5、EditTextDemoActivity .java
public class EditTextDemoActivity extends Activity {
EditText mBase;
EditText mNextAvailableText;
EditText mNotFullScreenSoftKeyboard;
Button mGoNext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUI();
}
private void initUI(){
mBase = (EditText) findViewById(R.id.base);
mBase.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
mNextAvailableText = (EditText) findViewById(R.id.goNextAvailableEditText);
mNotFullScreenSoftKeyboard = (EditText)findViewById(R.id.notFullScreen);
mNextAvailableText.setOnEditorActionListener(new OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
mBase.setText("enter键按下,KeyEvent.keyCode="+event.getKeyCode()+
"+++++actionId="+actionId);
mNotFullScreenSoftKeyboard.requestFocus();
return true;
}});
mGoNext = (Button) findViewById(R.id.goNext);
mGoNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(EditTextDemoActivity.this,SecondActivity.class));
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_ENTER:
mBase.setText("enter键按下,被捕捉!。。。");
Toast.makeText(this, "软键盘enter键被按下!。。。", 10000); break;
}
return super.onKeyDown(keyCode, event);
}
}
6、SecondActivity .java
public class SecondActivity extends Activity {
EditText mUneditable;
EditText mDate;
EditText mActionGo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
initUI();
}
private void initUI(){
mUneditable = (EditText) findViewById(R.id.uneditable);
mDate = (EditText) findViewById(R.id.date);
mDate.setOnEditorActionListener(new OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId,KeyEvent event) {
mUneditable.setText("enter键按下,KeyEvent.keyCode="+event.getKeyCode()+
"+++++actionId="+actionId);
return false;
}});
mDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mUneditable.setText("OnClickListener");
hideKeyboard(mDate);
}});
mDate.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mUneditable.setText("setOnLongClickListener");
hideKeyboard(mDate);
return true;
} });
mDate.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mUneditable.setText("setOnTouchListener");
hideKeyboard(mDate);
mDate.requestFocus();
return true;
}});
mActionGo = (EditText) findViewById(R.id.actionGoet);
mActionGo.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {
mDate.setText(s);
mDate.setSelection(start+count);
}});
}
private void hideKeyboard( View v){
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
. hideSoftInputFromWindow(v.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
}
这结详细介绍了EditText的大部分特性和常用功能,如常用的密码框,输入类型等等。如有不妥之处,欢迎交流,谢谢。