简体   繁体   中英

Changing the values from EditText programmatically

I have a decimal number in EditText and I'm trying to change it to always show a decimal part but the user doesn't be able to change the decimal part, only the integer part has to be editable. The decimal part is always a default value.

Example: I have the number 2.025,50 at EditText, if I delete all the digits I ll have 0,50. If I write 10 , I ll have 0,50. If I write 10 , I ll have 10,50.

Can anyone help me out ??

I created a function you can use for this, so you just input your number with a decimal and it will give you the decimal part of the number. Use editText changed listener. So when u pass the value being typed by a user call this function and pass the numberWithTheFraction to the function getFractionalPart and add the userinput as shown in the code bellow.

   private static double getFractionalPart(double num) {     
       if (num > 0) {
          return num - Math.floor(num);
       } else {
          return ((num - Math.ceil(num)) * -1);
}
}

you can have a look at this example for an example of editText change listener.

So in the above example when you say textView.setText(getFractionalPart(numberWithTheFraction)+userInput)

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