본문 바로가기

IT_Story/안드로이드

Android TextWatcher

android TextWatcher

EditText에 글씨가 입력이 될 때마다 입력된 값을 받아서 처리해준다.



예)

EditText mXMLBuyCount = null;

mXMLBuyCount = (EditText)findViewById(R.id.etBuyCount);

 

//선언

TextWatcher watcher = new TextWatcher()

{

    @Override

    public void afterTextChanged(Editable s) {

         //텍스트 변경 후 발생할 이벤트를 작성.

    }

 

    @Override

    public void beforeTextChanged(CharSequence s, int start, int count, int after)

    {

         //텍스트의 길이가 변경되었을 경우 발생할 이벤트를 작성.

    }

 

    @Override

    public void onTextChanged(CharSequence s, int start, int before, int count)

    {

         //텍스트가 변경될때마다 발생할 이벤트를 작성.

         if (mXMLBuyCount.isFocusable())

         {

             //mXMLBuyCount EditText 가 포커스 되어있을 경우에만 실행됩니다.

         }

    }

}

 

//호출

mXMLBuyCount.addTextChangedListener(watcher);