Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear text in EditText when entered [duplicate]

I'm trying to set and onclicklistener so that when I click within the edittext element it will clear its current contents. Is there something wrong here? When I compile this code I get a force quit and ActivityManager: Can't dispatch DDM chunk 4d505251: no handler defined error.

public class Project extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    EditText editText = (EditText)findViewById(R.id.editText1);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        editText.setOnClickListener(this);

        setContentView(R.layout.main);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        editText.setText("");
    }
}
like image 622
locoboy Avatar asked Mar 15 '11 06:03

locoboy


3 Answers

Also you can use code below

editText.getText().clear();
like image 132
Serhii Nadolynskyi Avatar answered Oct 13 '22 10:10

Serhii Nadolynskyi


First you need to call setContentView(R.layout.main) then all other initialization.

Please try below Code.

public class Trackfolio extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    public EditText editText;

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

        setContentView(R.layout.main);

        editText = (EditText) findViewById(R.id.editText1);
        editText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        editText.getText().clear(); //or you can use editText.setText("");
    }
}
like image 37
Chirag Avatar answered Oct 13 '22 08:10

Chirag


just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box.

like image 45
user2088464 Avatar answered Oct 13 '22 08:10

user2088464