Skip to content Skip to sidebar Skip to footer

How To Clear The Edittext When Onclick On Button

How do I clear the data which is given to an EditText dynamically when clicking the button? How do I write the code? What function or method do I use?

Solution 1:

Try

publicvoidclear(View v) {
     edittext.setText("");
 } 

Where clear is registered as onclick handler for the button in the layout file like this

<ImageButton android:id="@+id/ClearButton"
        android:text="@string/ClearButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clear"           <<<--------here
        android:src="@drawable/clear"
    />

Solution 2:

Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(newView.OnClickListener(){
    publicvoidonClick(View v){
        EditText et=(EditText) findViewById(R.id.et);
        et.setText("");
    }
});

Solution 3:

Its simple, I'm just considering that u know how to create the XML file... I'm putting across the java part of it....

publicclassStackOverFlowextendsActivity{

    Button buttonToClick;
    EditText editTextToclear;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    buttonToClick.setOnClickListener(newView.OnClickListener() {
        publicvoidonClick(View v) {

            editTextToclear.setText("");


        }
    });
    }

}

Solution 4:

Solution 5:

Register an OnClickListener for the button which deletes the text in the EditText. This document describes how to do that: http://developer.android.com/guide/topics/ui/ui-events.html

To clear the text in the listener just set an empty text: yourButton.setText("");

Post a Comment for "How To Clear The Edittext When Onclick On Button"