2

I know something similar has already been asked, but I'm having some trouble to get a decimal number that come from keyboard.

My Java code in the onCreate method should be:

textS0 = (EditText)findViewById(R.id.editS0);
Button btn_S0 = (Button)findViewById(R.id.getS0);

btn_S0.setOnClickListener(new View.OnClickListener() 
{
    public void onClick(View v)
    {
        //how should I get here the number from keyboard?
        //I think  I should be something like
        //double S0=textS0.getText()....
    }
});

And that's what my XML file contains

<EditText
       android:id="@+id/editS0"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:ems="10"
       android:hint="@string/S0"
       android:inputType="numberDecimal" />
<Button
       android:id="@+id/getS0"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/setS0" />
</LinearLayout>
user123
  • 8,970
  • 2
  • 31
  • 52
SciPhy
  • 143
  • 1
  • 1
  • 10
  • 1
    You'll have to parse the string to a double: [`Double.parseDouble()`](http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#parseDouble(java.lang.String)) – Lukas Knuth May 12 '13 at 11:15
  • when you do getText() it returns you String, just do a simple Double.parseDouble() to get your desired value – Chor Wai Chun May 12 '13 at 11:15
  • try{Double.parseDouble(textS0.getText().toString())}catch(NumberFormatException e){e.printStacktrace();} – Raghunandan May 12 '13 at 11:20
  • @ChorWaiChun, [`EditText.getText()`](http://developer.android.com/reference/android/widget/EditText.html#getText()) returns an [`Editable`](http://developer.android.com/reference/android/text/Editable.html) object that itself contains the content. You should call `EditText.getText().toString()` to retrieve the juicy String innards. – indivisible Nov 15 '14 at 17:57

3 Answers3

8

Just do this:

double S0 = Double.parseDouble(textS0.getText().toString());
  • If string is empty, causes `java.lang.NumberFormatException: empty String`. https://stackoverflow.com/questions/21542079/java-lang-numberformatexception-empty-string – O-9 Jul 30 '19 at 12:26
3
textS0=(EditText)findViewById(R.id.editS0);
Button btn_S0=(Button)findViewById(R.id.getS0);

btn_S0.setOnClickListener(new View.OnClickListener() 
{
public void onClick(View v)
{

           double S0 = Double.parseDouble(textS0.getText().toString());
}
});
pedja
  • 3,285
  • 5
  • 36
  • 48
0

May b you face null pointer exception in string so try this....

  1. get the number decimal from edittext using this

       Double BText=ParseDouble(String.valueOf(edittext.getText()));
    
  2. after that paste this code.. it prevents you from null pointer exception

    double ParseDouble(String strNumber) {
    if (strNumber != null && strNumber.length() > 0) {
        try {
            return Double.parseDouble(strNumber);
        } catch(Exception e) {
            return -1;
        }
    }
    else return 0;
    }
    
Sunil
  • 3,785
  • 1
  • 32
  • 43