Note that there are some explanatory texts on larger screens.

plurals
  1. POPrevent edittext from being empty
    primarykey
    data
    text
    <p>I have the following code which I want to use to make sure that my edittext wont be empty. So if the first drawn 0 (zero) is removed it must revert to 0 when the focus changes, Here is the app so far:</p> <pre><code>package your.test.two; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class TesttwoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); EditText edtxt = (EditText)findViewById(R.id.editText1); // if I don't add the following the app crashes (obviously): edtxt.setText("0"); edtxt.setOnFocusChangeListener(new View.OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { // TODO Auto-generated method stub update(); } }); } public void update() { EditText edittxt = (EditText)findViewById(R.id.editText1); Integer i = Integer.parseInt(edittxt.getText().toString()); // If i is an empty value, app crashes so if I erase the zero //on the phone and change focus, the app crashes } } </code></pre> <p>I have tried the following in the update() method:</p> <pre><code>String str = edittxt.getText().toString(); if (str == "") { edittxt.setText("0"); } </code></pre> <p>But it doesn't work. How can I allow the edittext to never be emty, revert to zero when empty but not when a value exists. I have already made sure that the edittext can only allow numerical values.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload