简体   繁体   中英

Android EditText Numberdecimal user input read

I am looking for a sample code to read user input for numberDecimal input type. XML Code (provided below) Java code to read the user input. Thanks.

    android:id="@+id/editText1"
    android:layout_width="368sp"
    android:layout_height="35sp"
    android:layout_marginTop="8sp"
    android:background="#9E9E9E"
    android:hint="Enter Initial Bill Total"
    android:gravity="center_vertical|center_horizontal"
    android:ems="10"
    android:inputType="numberDecimal"
    android:textSize="32dp"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    android:layout_marginLeft="8sp"
    android:layout_marginRight="8sp"
    app:layout_constraintLeft_toLeftOf="parent"

Use numberSigned along with numberDecimal (assuming you are interested in positive integers only otherwise numberDecimal is fine):-

android:inputType="numberDecimal|numberSigned"

You can also set it up from Java src:-

edit_text.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);

Now to read the value entered do this:-

EditText mEdit   = (EditText)findViewById(R.id.editText1);
String value = mEdit.getText().toString();

Checked and tested this code, it works fine.

    String str = editText.getText().toString();

    float f = Float.parseFloat(str);

    double d = Double.parseDouble(str);

    System.out.println("str :" + str + "\nfloat :" + f+ "\ndouble :" + d);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM