如何使用数据绑定更新 textview、ontextchanged in edittext android
how to update textview, ontextchanged in editext using Databinding android
我有编辑文本,将从用户那里获取输入
<EditText
android:inputType="number"
android:id="@+id/etQuantity"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/button_background"
android:backgroundTint="@color/green"
android:hint="Quantity"
android:padding="5dp"
android:onTextChanged="@{inspect::onQuantityTextChanged}"
android:textColor="@color/lightblack" />
<TextView
android:id="@+id/tvCalculatedTotal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/button_background"
android:backgroundTint="@color/green"
android:hint="Total"
android:inputType="number"
android:padding="5dp"
android:text="@={inspect.calculatedTotal}"
android:textColor="@color/lightblack" />
我想将其设置为文本视图 onTextchanged in edit text
class InspectViewModel(repository: UserRepository) : ViewModel() {
var calculatedTotal: String = ""
fun onQuantityTextChanged(
s: CharSequence,
start: Int,
before: Int,
count: Int
) {
calculatedTotal= s.toString()
}
}
我得到了值,但它没有更新文本视图,我使用了实时数据但得到了 ANR
因为这一行,您获得了 ARN android:text="@={inspect.calculatedTotal}"
为您的 EditText
使用 android:text="@={inspect.calculatedTotal}"
和 android:text="@{inspect.calculatedTotal}"
为您的 TextView
而 calculatedTotal
是 MutableLiveData
我有编辑文本,将从用户那里获取输入
<EditText
android:inputType="number"
android:id="@+id/etQuantity"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/button_background"
android:backgroundTint="@color/green"
android:hint="Quantity"
android:padding="5dp"
android:onTextChanged="@{inspect::onQuantityTextChanged}"
android:textColor="@color/lightblack" />
<TextView
android:id="@+id/tvCalculatedTotal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/button_background"
android:backgroundTint="@color/green"
android:hint="Total"
android:inputType="number"
android:padding="5dp"
android:text="@={inspect.calculatedTotal}"
android:textColor="@color/lightblack" />
我想将其设置为文本视图 onTextchanged in edit text
class InspectViewModel(repository: UserRepository) : ViewModel() {
var calculatedTotal: String = ""
fun onQuantityTextChanged(
s: CharSequence,
start: Int,
before: Int,
count: Int
) {
calculatedTotal= s.toString()
}
}
我得到了值,但它没有更新文本视图,我使用了实时数据但得到了 ANR
因为这一行,您获得了 ARN android:text="@={inspect.calculatedTotal}"
为您的 EditText
android:text="@={inspect.calculatedTotal}"
和 android:text="@{inspect.calculatedTotal}"
为您的 TextView
而 calculatedTotal
是 MutableLiveData