How to set default value 0 in edittext when nothing is entered?

I have an application that adds a number. I have 4 edittexts here. I want that when I do not enter any of one of the edittexts, it suggests that I enter 0. How can this be done? Here is my code:

public class Order extends Activity { Button GoBackHome; private Button button1; private EditText txtbox1,txtbox2,txtbox3,txtbox4; private TextView tv; Button PayNow; @Override public void onBackPressed() { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.order); GoBackHome = (Button) findViewById(R.id.gohomebutton); PayNow = (Button) findViewById(R.id.button2); txtbox1= (EditText) findViewById(R.id.editText1); button1 = (Button) findViewById(R.id.button1); tv = (TextView) findViewById(R.id.editText5); txtbox2= (EditText) findViewById(R.id.editText2); txtbox3= (EditText) findViewById(R.id.editText3); txtbox4= (EditText) findViewById(R.id.editText4); button1.setOnClickListener(new clicker()); GoBackHome.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final Intent i = new Intent(Order.this, MainActivity.class); startActivity(i); } }); PayNow.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final Intent i = new Intent(Order.this, Payment.class); startActivity(i); } }); } class clicker implements Button.OnClickListener { public void onClick(View v) { String a,b,c,d; Integer vis; a = txtbox1.getText().toString(); b = txtbox2.getText().toString(); c = txtbox3.getText().toString(); d = txtbox4.getText().toString(); vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3+Integer.parseInt(c)*4+Integer.parseInt(d)*5; tv.setText(vis.toString()); } } } 
+4
source share
5 answers

You can do as Tushar said, or you can initialize the value in XML. Sort of

 <EditText android:name="@+id/editText1" android:text="0"/> 

You can also find FYI cleaner to handle clicks on the xml button, for example:

 <Button android:name="@+id/btn1" android:onClick="handleClicks"/> 

and then in java you will have a public void method:

 public void handleClicks(View clickedView){ if(clickedView.getId() == btn1.getId(){ ... } else if (...){} } 
+6
source

initialize as:

 txtbox1.setText("0"); 
+1
source

You can set android:hint="0" in your XML file, and then in your code you can check if it is empty (possibly using TextUtils.isEmpty() ) and set some variable to 0.

android:hint="0" will make "0" in your EditText s, but "0" will disappear when something is entered.

Then you can change onClick() to the following:

 class clicker implements Button.OnClickListener { public void onClick(View v) { String a,b,c,d; Integer vis; a = txtbox1.getText().toString(); b = txtbox2.getText().toString(); c = txtbox3.getText().toString(); d = txtbox4.getText().toString(); try { vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3+Integer.parseInt(c)*4+Integer.parseInt(d)*5; tv.setText(vis.toString()); } catch (NumberFormatException e) { vis = "0"; } // Do something with "vis" } } 

Or you can create a method to check the value, try to parse to int or return the default value.

 public int getInt(String edtValue, int defaultValue) { int value = defaultValue; if (edtValue != null) { try { value = Integer.parseInt(edtValue); } catch (NumberFormatException e) { value = defaultValue; } } return value; } 

Then you change your call to

 vis = this.getInt(a, 0) * 2 + this.getInt(b, 0) * 3 + this.getInt(c, 0) * 4 + this.getInt(d, 0) * 5; 
+1
source

Check the length of the EditText when you get it

 String value = null; if(ed.getText().length()){ value = textBox.getText().toString(); } else value = 0+""; 
+1
source

Thank you for your help, but there is one missing in your code) immediately after btn1.getId ().

public void handleClicks (View clickedView) {if (clickedView.getId () == btn1.getId () ) {...} else if (...) {}}

0
source

Source: https://habr.com/ru/post/1499249/


All Articles