如何在Android中单击提交时使用数据绑定进行验证

问题描述

我有某种形式想要对其进行验证..我正在使用MVVM和数据绑定..现在,我可以逐字段进行验证..当我在字段中编写时,验证正在运行并显示错误消息。

我想停止该操作,并仅在单击提交..时显示所有字段的所有错误消息。 我该怎么做??

form.xml

from tkinter import *; # Import the Tk library.

import time; # So that we can use the time library
import math; # And perform maths operations

tk = Tk(); # Make a new window
tk.title("Not Undertale"); # Set the title of the window. You can change this if you like.
canvas = Canvas(tk,width=600,height=400); # Make a canvas that we can put the text on. You can play around with the numbers if you like.
canvas.pack(); # This basically tells Tk to find somewhere to put the canvas.
text = "         Hello. This goes on forever."; # This is the message that will scroll across the screen. You can change this,although I reccommend putting at least on space either before or after the text.
txt = canvas.create_text(300,200,text = "          ",font = ("Monospace",12)); # This creates the item that will hold the widget. Again,you can play around with the numbers. The font is Monospace size 12,this is optional. I reccommend using a fixed-width font for this though.

timer = 0; # The timer.
count = 0; # A variable that we will use later.

while True:
    try:
        tk.update(); # The reason this is in a Try/Except statement is because you get errors when you close the window,so we want to ignore that and finish the program.
    except:
        break;
    time.sleep(0.05); # Wait a bit before drawing the next frame
    timer += 0.05; # And add a bit to timer
    count = math.floor(timer * 4) % len(text); # This is where we use the count variable. Basically this changes by 1 every frame,we use this to find what part of the text we're writing.
    if count > len(text): # If the count variable is more than the length of the text,set it to 0.
        count = 0; # I'm positive we don't need this bit,but it's in there just in case.
    display_txt = ""; # This is a temporary variable where we store the scrolling letters.
    c = count; # copy the count variable to one called c
    while c < count + 10: # While c is between count and ten more than count.
        display_txt += text[c % len(text)]; # We add a letter to display_txt
        c += 1; # And add one to c.
    canvas.itemconfig(txt,text = display_txt); # The last thing we need to do is change the content of the text item so that it displays the scrolled text.

这是我的 DataBindingAdapter.java

<EditText

                android:id="@+id/companyName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:layout_marginBottom="121dp"
                android:ems="10"
                android:hint="Company Name"
                android:textColorHint="#bbb"
                android:inputType="textPersonName"
                android:text="@={quotes.companyName}"
                app:layout_constraintBottom_toTopOf="@+id/type"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <EditText

                android:id="@+id/recordNum"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:ems="10"
                android:hint="Record Number"
                android:textColorHint="#bbb"
                android:inputType="textPersonName"
                android:text="@={quotes.recordNum}"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/companyName" />

这是我的OnSubmitClick函数,位于package com.dynamic.rentalbooking.utils; import android.text.TextUtils; import android.widget.EditText; import androidx.databinding.BindingAdapter; public class DataBindingAdapter { @BindingAdapter("android:text") public static void text(EditText editText,String text) { // ignore infinite loops if (TextUtils.isEmpty(text)) { editText.setError(null); return; } if (editText.getText().toString().length() < 4) { editText.setError("This field must be minimum 4 length"); } else editText.setError(null); } }

viewmodel.java

请帮助..我是数据绑定和mvvm的新手,所以也许我迷失了一些概念

致谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)