Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <layout xmlns:android="http://schemas.android.com/apk/res/android">
- <data>
- <import type="java.math.BigDecimal"></import>
- <import type="com.arit.demo.mvvm.DecimalUtil"></import>
- <import type="android.view.View"></import>
- <variable name="qty" type="int"></variable>
- <variable name="user" type="com.arit.demo.mvvm.User"></variable>
- <variable name="handler" type="com.arit.demo.mvvm.ClickHandler"></variable>
- </data>
- <android.support.constraint.ConstraintLayout
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <EditText
- android:id="@+id/editText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:ems="10"
- android:inputType="textPersonName"
- android:text="@={DecimalUtil.convertToString(user.age)}"
- app:layout_constraintBottom_toTopOf="@+id/tvFirstname"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- android:layout_marginBottom="20dp"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@{DecimalUtil.convertToString(user.age), default = `18`}"
- android:visibility="@{(user.age.intValue() > 20) ? View.VISIBLE : View.GONE }"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- android:textSize="30sp"
- android:id="@+id/tvFirstname"/>
- <Button
- android:id="@+id/btnDoEvent"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Normal Click"
- android:onClick="@{()-> handler.onButtonClick(user)}"/>
- <Button
- android:id="@+id/btnDoEventByLambda"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="Lambda Click" />
- </android.support.constraint.ConstraintLayout>
- </layout>
- 2. DecimalUtil
- public class DecimalUtil {
- @InverseMethod("convertToString")
- public static BigDecimal stringToDecimal(String value){
- if(!value.isEmpty()) {
- return new BigDecimal(value);
- }else{
- return BigDecimal.valueOf(0);
- }
- }
- public static String convertToString(BigDecimal value){
- DecimalFormat formater = new DecimalFormat("#,###,###.00");
- return String.valueOf(formater.format(value));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement