我有一個活動中的4個編輯文本,輸入限制爲3個字符。
我想實現行爲,因爲用戶在以前的編輯文本中鍵入3個字符時 - 下一次編輯應該自動獲得焦點。 相反:當用戶從上次編輯文本的末尾開始刪除時 - 用戶可以刪除所有先前字段中的字符,而無需手動選擇每個編輯文本。
實現這個最簡單的方法是什麼?Android連續EditTexts
我只能想到爲每個EditText使用keyListenners,當它等於最大值時,檢查文本長度和開關焦點。
這是一種正確的方式還是有其他解決方案?
我有一個活動中的4個編輯文本,輸入限制爲3個字符。
我想實現行爲,因爲用戶在以前的編輯文本中鍵入3個字符時 - 下一次編輯應該自動獲得焦點。 相反:當用戶從上次編輯文本的末尾開始刪除時 - 用戶可以刪除所有先前字段中的字符,而無需手動選擇每個編輯文本。
實現這個最簡單的方法是什麼?Android連續EditTexts
我只能想到爲每個EditText使用keyListenners,當它等於最大值時,檢查文本長度和開關焦點。
這是一種正確的方式還是有其他解決方案?
我認爲這也是最好的方法。
我會嘗試一些像:
firstText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(CharSequence s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){
if(s.toString().length() == 3){
sedoncText.setFocusableInTouchMode(true);
sedoncText.requestFocus();
}
}
});
我沒有測試它,也許有些功能不完全正確拼寫。
希望它有幫助。
您可以使用TextWatcher進行此項工作。 短代碼片段:
editTextA.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// implement your logic here
// e.g. Set cursor to the end of another Textfield:
if (s.toString().length() > 2){
int position = editTextB.length();
editTextB.setSelection(position);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
對於你的任務,每一個需要的EditText自己TextWatcher(包括specifig邏輯)。
UPDATE:
短(未經測試)知道如何刪除當開關的EditText:
private boolean isEditTextVirgin;
isEditTextVirgin = true;
editTextB.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) {
// Check if the TextView is filled before editing
// if yes then save this information for later
if (s.toString().length() > 0){
isEditTextVirgin = false;
}
}
@Override
public void afterTextChanged(Editable s) {
// Check if the EditText is empty after a edit and if it was filled before
// if both yes then jump the the previous EditText
if (s.toString().length() == 0 && isEditTextVirgin == false){
int position = editTextA.length();
editTextA.setSelection(position);
// if you want you can reset your variable
// isEditTextVirgin = true;
}
}
});
要保存在一個成員變量狀態(如果曾經的EditText充滿)。
以及如何管理字符刪除,所以當我移動到前一個字段時刪除最後一個字段中的所有字符? – Androider
在EditText上添加
android:maxLength="3"
這將不允許超過3個字符輸入。
並把重點放在未來的EditText請參考以下鏈接
顯示你與現在的工作,包括佈局,請代碼。 –
使用TextWatcher,你也可以在這裏結算計數解決方案http://stackoverflow.com/questions/4310525/counting-chars-in-edittext-changed-listener –
謝謝。以及如何更好地切換到另一個edittext? – Androider