I’m kind of new to android programming and I’ve been searching for an answer to this for hours. I’m trying to get an EditText cleared when someone clicks on it, but for some reason eclipse isn’t recognizing the onClickListener.
What did I miss?
public class PillbugsStart extends Activity {
private EditText text;
private TextView f;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pillbugsstart);
text = (EditText) findViewById(R.id.editText1);
text.setOnClickListener(this);
}
@Override
public void onClick(View v) {
editText.setText("");
f = (TextView) findViewById(R.id.grah);
ImageButton b = (ImageButton)findViewById(R.id.testbutton2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View argo) {
Intent j = new Intent(PillbugsStart.this, Startpage.class);
PillbugsStart.this.startActivity(j);
}
});
}
public void myClickHandler(View view) {
switch (view.getId()) {
case R.id.FBLUB:
RadioButton pounds = (RadioButton) findViewById(R.id.lbs);
RadioButton kilograms = (RadioButton) findViewById(R.id.kilograms);
if (text.getText().length() == 0)
{
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (pounds.isChecked())
{
f.setText(String.valueOf(convertFromPounds(inputValue)));
pounds.setChecked(true);
}
else
{
f.setText(String.valueOf(convertFromKilograms(inputValue)));
}
break;
}
}
private float convertFromPounds(float pounds)
{
return ((pounds * 454) * 1/2);
}
private float convertFromKilograms(float kilograms)
{
return ((kilograms * 1000) * 1/2);
}
}
Clearing the Text on Click event on
EditTextis not a good UserExperience approach.and Even if you succeed to put
onClicklistener onEditText, you will face problem of focus and keyboard not appearing.You can Wrap it around using a
RelativeLayoutand putEditTextas center and Button with text as"x"aligned to right inRelativeLayoutlike this. Button will appear above theEditTextand can be made looking nice with minor tweaking..clear the Text onClick of the
Button“X”