Advertisement
irobust

DecimalUtil for Android Data Binding Library

Sep 14th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. 1. activity_main.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <layout xmlns:android="http://schemas.android.com/apk/res/android">
  4. <data>
  5. <import type="java.math.BigDecimal"></import>
  6. <import type="com.arit.demo.mvvm.DecimalUtil"></import>
  7. <import type="android.view.View"></import>
  8. <variable name="qty" type="int"></variable>
  9. <variable name="user" type="com.arit.demo.mvvm.User"></variable>
  10. <variable name="handler" type="com.arit.demo.mvvm.ClickHandler"></variable>
  11. </data>
  12. <android.support.constraint.ConstraintLayout
  13. xmlns:app="http://schemas.android.com/apk/res-auto"
  14. xmlns:tools="http://schemas.android.com/tools"
  15. android:layout_width="match_parent"
  16. android:layout_height="match_parent"
  17. tools:context=".MainActivity">
  18. <EditText
  19. android:id="@+id/editText"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:ems="10"
  23. android:inputType="textPersonName"
  24. android:text="@={DecimalUtil.convertToString(user.age)}"
  25. app:layout_constraintBottom_toTopOf="@+id/tvFirstname"
  26. app:layout_constraintLeft_toLeftOf="parent"
  27. app:layout_constraintRight_toRightOf="parent"
  28. android:layout_marginBottom="20dp"/>
  29.  
  30. <TextView
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:text="@{DecimalUtil.convertToString(user.age), default = `18`}"
  34. android:visibility="@{(user.age.intValue() > 20) ? View.VISIBLE : View.GONE }"
  35. app:layout_constraintBottom_toBottomOf="parent"
  36. app:layout_constraintLeft_toLeftOf="parent"
  37. app:layout_constraintRight_toRightOf="parent"
  38. app:layout_constraintTop_toTopOf="parent"
  39. android:textSize="30sp"
  40. android:id="@+id/tvFirstname"/>
  41.  
  42. <Button
  43. android:id="@+id/btnDoEvent"
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:text="Normal Click"
  47. android:onClick="@{()-> handler.onButtonClick(user)}"/>
  48.  
  49. <Button
  50. android:id="@+id/btnDoEventByLambda"
  51. android:layout_width="match_parent"
  52. android:layout_height="wrap_content"
  53. android:text="Lambda Click" />
  54.  
  55.  
  56. </android.support.constraint.ConstraintLayout>
  57. </layout>
  58.  
  59. 2. DecimalUtil
  60. public class DecimalUtil {
  61.  
  62. @InverseMethod("convertToString")
  63. public static BigDecimal stringToDecimal(String value){
  64. if(!value.isEmpty()) {
  65. return new BigDecimal(value);
  66. }else{
  67. return BigDecimal.valueOf(0);
  68. }
  69. }
  70. public static String convertToString(BigDecimal value){
  71. DecimalFormat formater = new DecimalFormat("#,###,###.00");
  72. return String.valueOf(formater.format(value));
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement