简体   繁体   English

添加多个约束

[英]Adding more than one Constraint

I have created a business upload form using android studio linked to firebase database, I am facing an issue where i could only add a constraint on the (name=empty display "please fill all fields"), but how can i add this to every other EditText field, so when the user does not fill them all the message shows. 我使用链接到Firebase数据库的android studio创建了一个业务上载表单,我遇到一个问题,我只能在(name = empty display“ please fill all fields”)上添加一个约束,但是我该如何将其添加到每个其他EditText字段,因此当用户不填写它们时,将显示所有消息。 here is the code 这是代码

private void addBusiness(){

  String  name = businame.getText().toString().trim();
  String  email = busiemail.getText().toString().trim();
  String  number = businum.getText().toString().trim();
  String  address = busiadd.getText().toString().trim();
  String  info = busiinfo.getText().toString().trim();
  String  type = spinnerType.getSelectedItem().toString();

    if (!TextUtils.isEmpty(name)) {
      String id= databaseBusiness.push().getKey();
      Business business = new Business(id, name, email, number, address, info, type);
      databaseBusiness.child(id).setValue(business);
      Toast.makeText(this,"Upload Done", Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(this,"Please Fill All Fields",Toast.LENGTH_LONG).show();
    }
}

A simple solution could look like this: 一个简单的解决方案如下所示:

Map<String, String> userInputByTag = new HashMap<>();
userInputByTag.put("name", businame.getText().toString().trim());
userInputByTag.put("email", busiemail.getText().toString().trim());
...

And now you can iterate the map entries. 现在,您可以迭代地图条目。 If you find a map value that is empty, you can put up a message (using the corresponding map key). 如果找到一个空的映射 ,则可以显示一条消息(使用相应的映射键)。 Not beautiful, but definitely better compared to 不漂亮,但绝对比

 if (!TextUtils.isEmpty(name)) {
  ...
 else if(!TextUtils.isEmpty(email)) {
  ...

In other words: the straight forward but ugly solution would be a cascade of such if/else if/... statements. 换句话说:直截了当但丑陋的解决方案将是级联的if / else if / ...语句。

Beyond that - I am pretty sure that Android has some framework support for field validation. 除此之外-我很确定Android具备一些用于字段验证的框架支持。 You should do some research there, instead of re-inventing the wheel. 您应该在那里进行一些研究,而不是重新发明轮子。 Start reading here or there or that . 开始阅读这里那里

Convenient for the user: 为用户方便:

I would use the more convenient way for the user and display beforehand an error directly in the EditText, if it is empty, with using a TextWatcher on every EditText . 我会用更便捷的方式为用户直接在显示的EditText事先的错误,如果它是空的,与使用TextWatcherEditText

TextWatcher TextWatcher

If you detect that the EditText is empty ie in onTextChanged() you can call editText.setError(yourString); 如果您检测到EditText为空,即在onTextChanged() ,则可以调用editText.setError(yourString); .

I recommend to use an AppCompatEditText with a surrounded TextInputLayout . 我建议将AppCompatEditTextTextInputLayout一起使用。

But nevertheless, before submitting, you have to do this for every member again. 但是,尽管如此,在提交之前,您必须再次为每个成员执行此操作。

Either you have a more user friendly approach with dedicated errors, like: 您可能会遇到一种更人性化的错误提示,例如:

if (TextUtils.isEmpty(name)) {

//dedicated error message
return;
}

if (TextUtils.isEmpty(email )) {
//dedicated error message 
return;
}

//and so on

(...)
//if every field holds the correct information
databaseBusiness.child(id).setValue(business);
(...)

or you could just check for every member in one if statement and display a generic error message like you already have. 或者您可以只检查一个if语句中的每个成员,然后像您已经显示的那样显示一般错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM