android 之EditText輸入檢測

2021-07-03 03:09:35 字數 3213 閱讀 9655

最近開發乙個功能的時候發生乙個故事,其情節如下:

功能其實不複雜,其中需要乙個edittext來獲取使用者輸入的資訊.於是,我做了乙個dialog來顯示我的輸入介面(**如下):

malertdialog = new alertdialog.builder(this)//, android.r.style.theme_holo_light

.seticon(r.drawable.ic_dialog_info_light)

.settitle(r.string.model_rename_device)

.setview(createdialogview(devicename))

.setpositivebutton(android.r.string.ok,

new dialoginte***ce.onclicklistener()

}).setnegativebutton(android.r.string.cancel,

new dialoginte***ce.onclicklistener()

}).setondismisslistener(new dialoginte***ce.ondismisslistener() else}})

.create();

malertdialog.getwindow().setsoftinputmode(

windowmanager.layoutparams.soft_input_state_always_visible);

malertdialog.show();

private view createdialogview(string devicename) );

mdevicenameview.settext(devicename); // set initial value before adding listener

mdevicenameview.addtextchangedlistener(this);

mdevicenameview.setoneditoractionlistener(new textview.oneditoractionlistener() else

}});

mdevicenameview.setselection(mdevicenameview.length());

return view;

}

實現起來很簡單!不過當我把使用者輸入的字串儲存起來時候,問題就來了!

原來這個使用者輸入的字串需要儲存在一段我們自己配置的nv裡面,重要的是,分給我儲存的字串的空間只有20個byte,沒有錯就是byte. 所以很簡單,輸入的字元最多不能超過20個字元,由於是byte型別,所以對於輸入的字元必須做檢測,其字元必須在乙個byte取值空間(0-127)裡面.實際上就是asic碼.

所以需要對輸入的字元檢測.

為了能夠實時檢測edittext輸入的字元你需要edittext.addtextchangedlistener()來新增乙個textwatcher的檢測器,並且實現其中的方法:

public void aftertextchanged(editable s)

public void beforetextchanged(charsequence s, int start, int count, int after)

public void ontextchanged(charsequence s, int start, int before, int count)

首當其衝想到的辦法是在aftertextchanged方法裡面判斷當前輸入的字元是否時正確的字元,如果不是就通過editable s來更改:s.delete()來刪除.或者直接使用這個edittext的去從新設定輸入的字元:settext();

其實,這兩個辦法都不行,當快速輸入不對的字元時候就會出現異常,很顯然時同步的問題.

很快我給出來另個解決方法:在ontextchanged()裡面檢測是否有異常的字元,如果有則通過handler傳送訊息的形式來處理.

public void ontextchanged(charsequence s, int start, int before, int count) 

} "ontextchanged str="+s.tostring()+"start="+start+"; before="+before+"; count="+count);

}

handler mhandler = new handler() 

}};

private void inpttext_error_correction(charsequence chars)

str_b.deletecharat(i);}}

mdevicenameview.settext(str_b.tostring());

if(start_indx < 0)

mdevicenameview.setselection(start_indx);

toast.maketext(rename_model_activity.this, getstring(r.string.set_name_error_character_notice),

toast.length_short).show();

} }

最後要說的是:對於輸入字元的限制可以通過edittext.setfilters()來配置:

mdevicenameview.setfilters(new inputfilter );

model_name_max_length_bytes時輸入字元的最大長度,utf8bytelengthfilter是inputfilter的子類.這裡就是對輸入長度的適配.

其實你會很快發現!inputfilter就是乙個對輸入字元的檢測器.所以對於輸入字元錯誤檢測其實不用那麼麻煩,其實inputfilter完全可以解決.實現他的方法:

public charsequence filter(charsequence source, int start, int end,

spanned dest, int dstart, int dend)

對於輸入的錯誤字元,位元組返回""就可以:

for (int i = start; i < end; i++)

}

Android開發之輸入框EditText

現在先簡單介紹一下技術點 1.如何使用圓角輸入框和按鈕背景 2.如何實現 手機號 密碼 後面的豎線 3.如何巢狀輸入框的布局 4.如何監聽輸入框的輸入事件及刪除按鈕的動態顯示隱藏 1.如何使用圓角輸入框和按鈕背景 安卓為開發者準備了shape這個xml標籤,用於自定義一些形狀。那麼我就來定義乙個白色...

ScrollView中巢狀EditText滑動問題

在edittext中設定了最大行數,但是內容超果了最大行數限制,這時edittext是可以滑動的。但是如果在edittext巢狀在scrollview中時,會使edittext的滑動事件失效。解決方法為 edittext.setontouchlistener new view.ontouchlist...

android之interpolator的用法詳解

acceleratedecelerateinterpolator 在動畫開始與結束的地方速率改變比較慢,在中間的時候加速 accelerateinterpolator 在動畫開始的地方速率改變比較慢,然後開始加速 anticipateinterpolator 開始的時候向後然後向前甩 anticip...