0

i have implemented onClickListener and the main activity of my app in a separate classes. The main classes contains some buttons and some edittext, and when the user press a cancel button, the edittext hint should be reset. main activity

public ButtonListener(Context cx , Button b, EditText f) {
    cntx = cx;
    button = b;
    et = f;
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
            //check wich button was pressed
    if(button.getId() == R.button.button_cancel) {
        et.setHint(R.string.et_nome);
    }
    else {
            //operations for other buttons

But it doesn't work. When i press cancel, the edittext it's not erased. In the main activity i've call the button with

Button b = (Button)findViewById(R.button.button_cancel);

and set the listener

b.setOnclickListener(new ButtonListener(this, b, edittext));

what's wrong?

giozh
  • 9,868
  • 30
  • 102
  • 183

2 Answers2

3

Try to set the text of it blank:

et.setText(" ");

You can view other methods here.

Another way you handle this is by hiding the button to eliminate it temporarily. Check out this thread.

Community
  • 1
  • 1
  • yes, it works. Could you explain me why i couldn't do with setHint()? – giozh Feb 10 '13 at 19:28
  • setHint() is a a permanent element when the view is loaded which is cleared by the user interaction. –  Feb 10 '13 at 19:36
1

You should change:

if(button.getId() == R.button.button_cancel)

to:

if(v.getId() == R.button.button_cancel)

so that the code checks the identifier of the element that raised the click event, not your member variable button.

Ameen
  • 2,576
  • 1
  • 14
  • 17
  • this shoub not be a problem, because i've cheched with a log message that the if statement it's called correctly – giozh Feb 10 '13 at 19:26
  • Well, it works but it's somewhat redundant. Consider instantiation of another `ButtonListener` class with a `Button` other than `R.button.button_cancel`. Now the if statement of that call will never be true. – Ameen Feb 10 '13 at 19:36