EditText 和TextView 的功能基本类似,他们之间的主要区别在于EditText 提供了可编辑的文本框。
类的继承关系图:java.lang.Object ------android.view.View----android.widget.TextView------android.widget.EditText
直接子类:AutoCompleteTextView, ExtractEditText
间接子类:MultiAutoCompleteTextView
<span style="font-size:14px;">android:inputType="textCapSentences"//仅第一个字母大小 android:inputType="textAutoCorrect"android:inputType="textAutoComplete"//前两个自动完成 android:inputType="textMultiLine"//多行输入 android:inputType="textImeMultiLine"//输入法多行(不一定支持) android:inputType="textNoSuggestions"//不提示 android:inputType="textUri"//URI格式 android:inputType="textEmailAddress"//电子邮件地址格式 android:inputType="textEmailSubject"//邮件主题格式 android:inputType="textShortMessage"//短消息格式 android:inputType="textLongMessage"android:inputType="textPersonName"//人名格式 android:inputType="textPostalAddress"//邮政格式 android:inputType="textPassword"//密码格式 android:inputType="textVisiblePassword"//密码可见格式 android:inputType="textWebEditText"//作为网页表单的文本格式 android:inputType="textFilter"//文本筛选格式 android:inputType="textPhonetic"//拼音输入格式 android:inputType="number"//数字格式 android:inputType="numberSigned"//有符号数字格式 android:inputType="numberDecimal"//可以带小数点的浮点格式 android:inputType="phone"//拨号键盘 android:inputType="datetime"android:inputType="date"//日期键盘 android:inputType="time"//时间键盘</span>
<span style="font-size:14px;"><!-- 用于输入数字的文本框 --> <EditText android:id="@+id/editText1" android:inputType="date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLength="40" android:hint="输入电话号码" android:textColorHint="#FF000000" android:phoneNumber="true" android:imeOptions="actionGo"></EditText> <!-- 用于输入密码的文本框 --> <EditText android:id="@+id/editText2" android:inputType="date" android:layout_width="fill_parent" android:layout_height="wrap_content" android:maxLength="40" android:hint="输入密码" android:textColorHint="#FF000000" android:password="true" android:imeOptions="actionSearch"></EditText></span>
这里例子:监听edittext的字数超过140设置监听
<span style="font-size:14px;">EditText content;//定义一个文本输入框
TextView hasnum;// 用来显示剩余字数
int num =140;//限制的最大字数
content =(EditText) findViewById(R.id.et_content);
hasnumTV =(TextView) findViewById(R.id.tv_num);
hasnumTV.setText(num+"");
//下面为EditText文本框添加监听
content.addTextChangedListener(newTextWatcher(){
privateCharSequence temp;
privateint selectionStart;
privateint selectionEnd;
publicvoid beforeTextChanged(CharSequence s,int start,int count,int after){
}
publicvoid onTextChanged(CharSequence s,int start,int before,int count){
temp = s;
}
publicvoid afterTextChanged(Editable s){
int number = num - s.length();
hasnumTV.setText(""+ number);
selectionStart = content.getSelectionStart();
selectionEnd = content.getSelectionEnd();
if(temp.length()> num){
s.delete(selectionStart -1, selectionEnd);
int tempSelection = selectionEnd;
content.setText(s);
content.setSelection(tempSelection);//设置光标在最后
}
}
});</span>
原文:http://blog.csdn.net/u013424496/article/details/51329501