Home » Android » android – How to clear the edittext when onclick on Button

android – How to clear the edittext when onclick on Button

Posted by: admin May 14, 2020 Leave a comment

Questions:

Possible Duplicate:
Clear text in EditText when entered

How to clear the data which is given to edittext dynamically when clicking the button.
How must I write the code? What function or method I must use?
Please can anyone help me in doing this?

How to&Answers:

Try

 public void clear(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"
    />

Answer:

Button btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){
        EditText et=(EditText) findViewById(R.id.et);
        et.setText("");
    }
});

Answer:

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

public class StackOverFlow extends Activity{

    Button buttonToClick;
    EditText editTextToclear;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    buttonToClick.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            editTextToclear.setText("");


        }
    });
    }

}

Answer:

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(“”);

Answer:

if your button name is : rstBtn

& edittext field name is :name

then insert an Onclick Event handler :

rstBtn.setOnClickListener( new OnClickListener() {          
public void onClick(View v){
            name.setText("");
        }
    });

Answer:

You can use function

myEditText.setText("");