【问题标题】:Mandatory fields in android [duplicate]android中的必填字段[重复]
【发布时间】:2017-10-14 07:00:27
【问题描述】:

您好,我正在制作一个用户可以订购书籍的应用程序。当他或她用他的第二个名字等填写订单并点击订单按钮应用程序时,使用 Intent,将切换到 SMS,从而通过短信购买书籍。但我希望能够,如果用户不小心忘记了填写所有字段,吐司弹出消息“请填写 XYZ 字段”。我使用了 if else,但是当某些字段为空时,发生了其他事情。我收到了 android 消息,提示我的应用程序需要关闭并返回到上一个活动。在我的 LogCat 中什么也没发生。没有错误信息。

这是我的代码:

public void kreiranjeNarudzbine(View v) {
    EditText editTextIme = (EditText) findViewById(R.id.ime);
    String imeNarucioca = editTextIme.getText().toString();
    EditText editTextPrezime = (EditText)findViewById(R.id.prezime);
    String prezimeNarucioca = editTextPrezime.getText().toString();
    EditText editTelefonNarucioca = (EditText)findViewById(R.id.telefon);
    String telefonNarucioca = editTelefonNarucioca.getText().toString();
    EditText editAdresaNarucioca = (EditText)findViewById(R.id.adresa);
    String adresaNarucioca = editAdresaNarucioca.getText().toString();
    EditText editGradNarucioca = (EditText)findViewById(R.id.grad);
    String gradNarucioca = editGradNarucioca.getText().toString();
    EditText editKolicina = (EditText)findViewById(R.id.kolicina);
    String narucenaKolicina = editKolicina.getText().toString();
    int kolicina = Integer.parseInt(narucenaKolicina);
    int cenaNarudzbine = cena(kolicina);
    String poruka = sumiranjeNarudzbine(imeNarucioca, prezimeNarucioca,telefonNarucioca,adresaNarucioca,gradNarucioca,cenaNarudzbine);
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address", "+381629647169");
    smsIntent.putExtra("sms_body",poruka);
    if(imeNarucioca.equals("")){
        Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
    }
    else if(prezimeNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();

    }
    else if(telefonNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
    }
    else if(adresaNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
    }
    else if(gradNarucioca.equals("")){
        Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
    }
    else if(narucenaKolicina.equals("")){
        Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
    }
    else{

        startActivity(smsIntent);}

}

【问题讨论】:

  • 请发帖minimal reproducible example。我建议在 Android Studio 中开始一个新项目并为一个 EditText 编写代码。
  • 你也应该看看 EditText.setError()

标签: android


【解决方案1】:

如前所述,您检查以确保它不为 null 或为空。

if(imeNarucioca!=null && imeNarucioca.equals("")){
    Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
}
else if(prezimeNarucioca!=null && prezimeNarucioca.equals("")){
    Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();

}
else if(telefonNarucioca!=null && telefonNarucioca.equals("")){
    Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
}
else if(adresaNarucioca!=null && adresaNarucioca.equals("")){
    Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
}
else if(gradNarucioca!=null && gradNarucioca.equals("")){
    Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
}
else if(narucenaKolicina!=null && narucenaKolicina.equals("")){
    Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
}
else{
    startActivity(smsIntent);
}

这可以转换成一种方法来阻止尽可能多的文本:

public boolean isETEmpty(EditText et){
    return (et != null && (et.equals("") || et.equals(" ")));//the final piece checks if the edittext is empty or just contains a space. It is or between
    //in order to ensure that one of those are true. Thus the parenthesis 
}

(how the above works)

现在,对于必填字段,您可以使用 HTML 来格式化文本:

/*your view*/.setText(Html.fromHtml("Your title. <font color='red'>*</font>"));

上述代码通过在必填字段后添加红色星号来格式化代码。以后你可以这样做:

/*your view*/.setText(Html.fromHtml("<font color='red'>*</font> Required field"));

注释 /*your view*/ 替换为 textview 引用、edittext 或用于设置文本的任何内容


进一步阅读:

https://stackoverflow.com/a/9161958/6296561

【讨论】:

  • 对我来说这总是正确的
【解决方案2】:

空编辑文本的字符串可能为空,这会导致问题。试试下面的代码。

if(imeNarucioca==null || imeNarucioca.equals("")){
        Toast.makeText(Narudzbina.this, "Unesite ime", Toast.LENGTH_LONG).show();
    }
    else if(prezimeNarucioca==null || prezimeNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite Prezime", Toast.LENGTH_LONG).show();

    }
    else if(telefonNarucioca==null || telefonNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite kontakt telefon", Toast.LENGTH_LONG).show();
    }
    else if(adresaNarucioca==null || adresaNarucioca.equals("")){
        Toast.makeText(Narudzbina.this,"Unesite adresu",Toast.LENGTH_LONG).show();
    }
    else if(gradNarucioca==null || gradNarucioca.equals("")){
        Toast.makeText(Narudzbina.this, "Navedite grad", Toast.LENGTH_LONG).show();
    }
    else if(narucenaKolicina==null || narucenaKolicina.equals("")){
        Toast.makeText(Narudzbina.this, "Navedite zeljenu kolicinu", Toast.LENGTH_LONG).show();
    }
    else{
        startActivity(smsIntent);}
   }

【讨论】:

  • 如果内容为空,请检查et.equals(null)。您正在检查 edittext 中的字符串是否为空,而不是 null。
【解决方案3】:

您应该使用android.text.TextUtils 中提供的isEmpty 方法。该方法的完整描述和实现:

/**
 * Returns true if the string is null or 0-length.
 * @param str the string to be examined
 * @return true if str is null or zero length
 */
public static boolean isEmpty(@Nullable CharSequence str) {
    if (str == null || str.length() == 0)
        return true;
    else
        return false;
}

因此,要验证 nullempty 用户输入,您可以执行以下操作:

if(android.text.TextUtils.isEmpty(imeNarucioca)){
    // ...
}

【讨论】:

    【解决方案4】:

    所以我在用这个,你可以随心所欲地使用它:

    方法是这样的:

    public static boolean checkEditTextIsEmpty(EditText... editTexts)
    {
        try
        {
            for (EditText editText : editTexts)
            {
                if (editText.getText().toString().trim().length() == 0)
                {
                    Drawable d = _application.currentActivity.getResources().getDrawable(android.R.drawable.ic_dialog_alert);
                    d.setBounds(0, 0, d.getIntrinsicWidth()/2, d.getIntrinsicHeight()/2);
                    editText.requestFocus();
                    editText.setError(editText.getHint() + " is empty", d);
                    return false;
                }
            }
        }
        catch (Exception ignored)
        {
            return false;
        }
        return true;
    }
    

    这是预览:(忽略我的风格和背景)

    我是这样实现的:

    if(checkEditTextIsEmpty(txtEmail, txtPassword))
    {
        //Do whatever if EditTexts is not empty
    }
    

    【讨论】:

      【解决方案5】:

      只需以编程方式将这一行添加到您的 Java 文件中, 在您的文本视图附近添加必填字段。

      tv.setText (Html.fromHtml(YourText
                      + " <font color='"
                      + getResources().getColor(R.color.colorAccent) + "'>" + " * "
                      + "</font>"));
      

      添加必填字段而不是 yourText 字段, 然后使用 colors.xml 中所需的颜色

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-06-29
        • 2014-08-02
        • 2021-12-13
        • 1970-01-01
        • 2015-02-26
        • 1970-01-01
        相关资源
        最近更新 更多