how to set the number format for an EditText

我们两清 提交于 2019-12-08 00:27:46

问题


I want the number being displayed in my EditText upto 1 decimal place with no leading zeroes e.g. 25.0 not like 025.0

how can i do this?

this is in android


回答1:


You can use DecimalFormat

DecimalFormat format = new DecimalFormat("##.#");
String formattedText = format.format(025.0);
editText.setText(formattedText);

You will get result in EditText as 25.0




回答2:


you can set it in xml file. Like

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="54dp"
    android:ems="10"
    android:inputType="numberDecimal" >


来源:https://stackoverflow.com/questions/11409979/how-to-set-the-number-format-for-an-edittext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!