本文實(shí)例講述了Android編程實(shí)現(xiàn)監(jiān)聽EditText變化的方法。分享給大家供大家參考,具體如下:
監(jiān)聽EditText中的內(nèi)容的變化。在EditText沒有找到一個(gè)setOnxxxx的方法。百度了一下,原來在EditText中有一個(gè)方法addTextChangedListner(TextWatcher watcher)方法,用他可以時(shí)時(shí)監(jiān)聽EditText的內(nèi)容變化。TextWatcher是一個(gè)接口類,所以必須實(shí)現(xiàn)TextWatcher里的抽象方法:

當(dāng)EditText里面的內(nèi)容有變化的時(shí)候,觸發(fā)TextChangedListener事件,就會(huì)調(diào)用TextWatcher里面的抽象方法。
et_pos.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 (s.length() != 0) { clearPosition.setVisibility(View.VISIBLE); }else { clearPosition.setVisibility(View.INVISIBLE); } }});希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選