android上需要的字段验证

时间:2014-02-02 16:00:03

标签: android validation

在android中如何创建必填字段验证?比如在一个页面上,使用可以将一些数据输入某些EditText。我想确保用户在继续之前输入内容,比如用户忘记输入某些数据,应用程序就不会崩溃。我只使用提供的输入类型进行了其他验证,如数字。但到目前为止,我研究我只找到了验证输入内容的方法,但没有找到输入的内容。 所以我应该在onCreate方法中添加一些内容,例如

if(EditText text is !=null){ do the stuff} else {error message}

但如果我这样做,应用程序运行的那一刻就会显示错误。 如何在c#中编写“EditText文本”我相信是TextBox文本。但我不知道如何在java android中做到这一点。我知道setText但不知道如何在不改变的情况下引用内容。

3 个答案:

答案 0 :(得分:0)

要在EditText中输入文字用户,您可以调用getText()方法。我建议您在用户单击某个按钮后执行验证。在EditText方法中验证onCreate()的内容是无用的。

答案 1 :(得分:0)

最好告诉用户他们需要尽可能地提供正确的信息。为什么?因为它允许用户快速识别问题并修复它。话虽这么说,我建议一旦它失去焦点,检查它的null或你想要的文本字段包含的任何内容。

textView.setOnFocusChangeListener(new OnFocusChangeListener() {          

    public void onFocusChange(View v, boolean hasFocus) {
        if(!hasFocus)
           //We get the text and then convert it to a string and check
           if(v.getText().toString() == ""){
           //Do what you want in this area
           }
    }
});

答案 2 :(得分:0)

String str = textview.getText().toString().trim()
相关问题