EditText类的基本结构

EditText 和TextView 的功能基本类似,他们之间的主要区别在于EditText 提供了可编辑的文本框。
类的继承关系图:java.lang.Object ------android.view.View----android.widget.TextView------android.widget.EditText

直接子类:AutoCompleteTextView, ExtractEditText

间接子类:MultiAutoCompleteTextView

常用的用法

Android editview在键盘上 editview在哪_xml

标签的主要属性

Android editview在键盘上 editview在哪_EditText_02

Android editview在键盘上 editview在哪_Text_03

Android editview在键盘上 editview在哪_xml_04

Android editview在键盘上 editview在哪_android_05

Android editview在键盘上 editview在哪_android_06

Android editview在键盘上 editview在哪_android_07

Android editview在键盘上 editview在哪_xml_08


设置输入的类型的 android:inputType=""


<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>

使用:

(1)xml中的使用:

<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>

(2)EditText的常用的监听之addTextChangedListener(new TextWatcher())

这里例子:监听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>

edittext和软件盘的弹出关系

EditText默认不弹出软件键盘

方法一:

在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden

例如:

<activity android:name=".Main"
 
 
                  android:label="@string/app_name"
 
 
                  android:windowSoftInputMode="adjustUnspecified|stateHidden"
 
 
                  android:configChanges="orientation|keyboardHidden">
 
 
            <intent-filter>
 
 
                <action android:name="android.intent.action.MAIN" />
 
 
                <category android:name="android.intent.category.LAUNCHER" />
 
 
            </intent-filter>
 
 
        </activity>

方法二:

让EditText失去焦点,使用EditText的clearFocus方法

例如:EditText edit=(EditText)findViewById(R.id.edit);

           edit.clearFocus();

方法三:

强制隐藏Android输入法窗口

例如:EditText edit=(EditText)findViewById(R.id.edit);  

           InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

           imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

EditText始终不弹出软件键盘

例:EditText edit=(EditText)findViewById(R.id.edit);

       edit.setInputType(InputType.TYPE_NULL);

相对布局中位于底部的控件不让他被软键盘弹出(底部的控件位于软键盘的顶部):

android:windowSoftInputMode="adjustPan"/>

不能设置下面类似的东西

//设置软件盘不弹出

  //getActivity().getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);)

EditText更改软件盘的enter键的功能

EditText通过设置 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

android:singleLine="true" 才行

xml的写法

1. <EditText   
2. "10dp"  
3. "match_parent"  
4. "wrap_content"  
5. "输入单位"  
6. "10dp"  
7. "10dp"  
8. "true"   
9. "actionSearch"  
10.         />

对于事件捕捉:(像下一项等一些就不用设置什么事件的捕捉了)

1. companySearchET.setOnEditorActionListener(new TextView.OnEditorActionListener() {    
2. public boolean onEditorAction(TextView v, int actionId,      
3.                    KeyEvent event)  {      
4. final String  searchStr = companySearchET.getText().toString().trim();  
5. if (actionId==EditorInfo.IME_ACTION_SEARCH    
6. null&&event.getKeyCode()== KeyEvent.KEYCODE_ENTER)) {      
7. //do something;    
8. if(searchStr == null || "".equals(searchStr)){  
9. this, "请输入单位关键字", Toast.LENGTH_SHORT).show();  
10. "这里", "这里");  
11. return true;  
12. else{  
13.                     }  
14. return true;    
15.                }      
16. return false;      
17.            }      
18.        });