EditText有個屬性setSelection(int start,int end);可以將文本框的文字選中。。
EditText的監聽與其他不太一樣,分別對應三個方法。
et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//改變時
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
//改變前
}
@Override
public void afterTextChanged(Editable s) {
//改變後
}
});
設置提示信息,et.setHint("這是提示信息");可以通過鍵盤事件setOnKeyListener來講編輯框內的文字進行處理,顯示在TextView上,
et.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// Toast.makeText(getApplicationContext(), "該按鍵的keyCode爲:"+keyCode,
// Toast.LENGTH_LONG).show();
if(et.getText().toString().length()>=11){
if(event.getAction()==KeyEvent.ACTION_UP){
String arg1 = et.getText().toString().substring(0, 4);
String arg2 = et.getText().toString().substring(4, 8);
String arg3 = et.getText().toString().substring(8, 11);
tv.setText(arg1+"-"+arg2+"-"+arg3);
}
}
return false;
}
});
下面介紹下inputStyle的對應格式:
android:inputType="none"
android:inputType="text"
android:inputType="textCapCharacters" 字母大寫
android:inputType="textCapWords" 首字母大寫
android:inputType="textCapSentences" 僅第一個字母大寫
android:inputType="textAutoCorrect" 自動完成
android:inputType="textAutoComplete" 自動完成
android:inputType="textMultiLine" 多行輸入
android:inputType="textImeMultiLine" 輸入法多行(如果支持)
android:inputType="textNoSuggestions" 不提示
android:inputType="textUri" 網址
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" 時間鍵盤