Jayizzle
Jayizzle

Reputation: 532

Android onClick function for an edittext, How do I call the edittext?

I have an edittext form that can get filled, I want to clear it upon an onClick of a text, so I put an onClick that goes to my clear function in main.

I however, dont know how to properly target the edittext that I want. I want to clear TWO of them.

public void clear(View v) {
    @+id/toptext.setText("");

} 

This is the XML for that specific text.

<TextView
android:id="@+id/toptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#303030"
android:text="@string/toptext"
android:textAppearance="?
android:attr/textAppearanceLarge"
android:textColor="#33B5E5"
android:textSize="50sp"
android:onClick="clear"  />

Upvotes: 2

Views: 5980

Answers (6)

Michael Yaworski
Michael Yaworski

Reputation: 13483

Create instances of your EditTexts, using the id of the EditTexts in your xml layout. Then use setText on them and make them blank.

public void clear(View v) {

    EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
    EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
    et.setText("");
    et2.setText("");
} 

Edit for some reason the method above didn't work. This was the end solution:

// Put one of these in your onCreate method:

TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnTouchListener(new View.OnTouchListener() { 
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
        EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
        et.setText("");
        et2.setText("");

        return true;
    }
});

// or

TextView tt = (TextView)findViewById(R.id.toptext);
tt.setOnClickListener(new View.OnClickListener() { 
    @Override
    public void onClick(View v) {

        EditText et = (EditText)findViewById(R.id.theIdOfYourEditText);
        EditText et2 = (EditText)findViewById(R.id.theIdOfYourOtherEditText);
        et.setText("");
        et2.setText("");
    }
});

Upvotes: 2

Mladen
Mladen

Reputation: 2210

You can use this code:

public void clear(View v) {
    if (v.getId() == R.id.toptext) {
        ((EditText) v).setText(null);
    }
}

Similarly, add another if condition for another EditText.

Upvotes: 1

codeMagic
codeMagic

Reputation: 44571

Use the param passed to the onClick() (v)

public void clear(View v) 
{
    ((TextView)v).setText("");
}

Assuming you want to clear the text in the TextView that you have your onClick attached to. The View passed into clear() is the View that was clicked so you just cast it to the appropriate View (TextView) then you can use it as normal.

So this will clear the text on whichever TextView was clicked.

Upvotes: 0

Manmohan Badaya
Manmohan Badaya

Reputation: 2336

I think instead of setting "", u should use null as.

TextView textView = (TextView) findViewById(R.id.toptext);
textView.setText(null);

Upvotes: 0

nKn
nKn

Reputation: 13761

Try this:

TextView tv = (TextView) findViewById(R.id.toptext);
tv.setText("Whatever you want!");

Upvotes: 1

Melquiades
Melquiades

Reputation: 8598

First, get reference to your TextView by calling findViewById:

TextView textView = (TextView) findViewById(R.id.toptext);

Then use it to clear:

textView.setText("");

Upvotes: 0

Related Questions