Thursday, 15 October 2015

Android: OnText Change Listner - To trigger event when the text in a text box changes

     In many cases you will have to monitor user actions to trigger certain events according to in. Lets consider a situation that you want to log into the app without a button click, when the user enters the right password you could trigger this function. In this tutorial you're gonna learn how to trigger an event when the user change the text on a textbox.



Java:
 EditText txtBox = (EditText ) findViewById(R.id. edittext1);  
 txtBox .addTextChangedListener(new TextWatcher()  
 {  
 @Override  
    public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {  
    // When user changed the Text  
    }  
    @Override  
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {  
    // TODO Auto-generated method stub  
 }  
    @Override  
    public void afterTextChanged(Editable arg0) {  
    // TODO Auto-generated method stub                     
    }  
 });  
           
XML:
  <TextView  
     android:id="@+id/textview1"  
     android:layout_width="match_parent"  
     android:layout_height="150dp"  
     android:hint="Description"  
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

No comments:

Post a Comment