我有一定的要求,使Android的EditText
採取月份和年份(所以我給了提示爲mm/yy)。當用戶輸入月份強制2位數字時,應該可見一個/
,例如(11 /)後面是年份。格式編輯文本需要月份和年份
以下是我在editext
上使用的代碼。但是,當我按下移動鍵盤上的十字按鈕時,它不會刪除/
,但會刪除輸入的月份。有人可以告訴我爲什麼是這種行爲?
ed.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) {
if(ed.getText().toString().matches("[0-9]") && ed.getText().toString().length()==2) {
month=ed.getText().toString();
ed.setText(month+"/");
}
}
});
嘿你這樣做的方式有點複雜。我對這個問題的建議是使用DatePickerDialog。 http://developer.android.com/reference/android/app/DatePickerDialog.html –