强制用户在Android中的EditText中输入内容

时间:2017-03-25 07:37:13

标签: android android-edittext

我的Activty中有几个EditTexts,我希望我的用户在提交表单之前正确输入。我该怎么做? 我还有微调器和RadioGroup Button。

4 个答案:

答案 0 :(得分:2)

您可以在提交按钮上添加验证点击:

private boolean validateFields() {
    int yourDesiredLength = 5;
    if (yourEditText1.getText().length() < yourDesiredLength) {
        yourEditText1.setError("Your Input is Invalid");           
        return false;
    }else if (yourEditText2.getText().length() < yourDesiredLength) {
        yourEditText1.setError("Your Input is Invalid");           
       return false;
    } 
    else{
        return true;
    }

开启提交按钮点击:

btnSubmit.setOnclickListener(new View.OnClickListener){
  @Override
        public void onClick(View v) {
            if(validateFields()){
                // Then Submit 
            }
        }
});

输出将是这样的。照片Credit

enter image description here

希望这有帮助。

答案 1 :(得分:1)

button.setOnClickListner

中对editText和所有其他组件应用验证

如果所有验证都满足,则进一步处理。

喜欢编辑文字验证

@ManytoOne

答案 2 :(得分:1)

试试这个:代码

  

我想在按下提交按钮之前检测到错误

在Editext上使用mkdir -p /tmp/jarupdate && cd /tmp/jarupdate find /usr/hdp/ -name "azure-storage*.jar" cp /usr/hdp/2.5.0.1-210/hadoop/lib/azure-storage-2.2.0.jar . cp /usr/hdp/current/spark-historyserver/lib/spark-assembly-1.6.3.2.5.0.1-210-hadoop2.7.3.2.5.0.1-210.jar . unzip azure-storage-2.2.0.jar jar uf spark-assembly-1.6.3.2.5.0.1-210-hadoop2.7.3.2.5.0.1-210.jar com/ mv -f spark-assembly-1.6.3.2.5.0.1-210-hadoop2.7.3.2.5.0.1-210.jar /usr/hdp/current/spark-historyserver/lib/spark-assembly-1.6.3.2.5.0.1-210-hadoop2.7.3.2.5.0.1-210.jar cd .. && rm -rf /tmp/jarupdate

setOnFocusChangeListener()

答案 3 :(得分:0)

我明白你的观点,你可以使用 TextInputLayout 代替 EditText

定义您的xml布局:

 <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp">
            <EditText android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:hint="Password"/>
        </android.support.design.widget.TextInputLayout>

然后您可以在按钮单击侦听器中使用帮助器方法来验证所有editText的输入数据

 public boolean validate() {
        boolean valid = true;
        if (password.isEmpty() || password.length() < 6) {
            _passwordText.setError("Password is too short !");
            valid = false;
        } else {
            _passwordText.setError(null);
        }

        return valid;
    }

不要忘记添加设计支持库依赖

compile 'com.android.support:design:25.1.1'

看到这个例子: Creating a Login Screen Using TextInputLayout

会是这样的 如果用户输入无效数据,它将以红线显示此警告 enter image description here

相关问题