通过程序动态启用或禁用特定EditTextView的验证



MainActivity。XML

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginTop="50dp">

    <android.support.v7.widget.CardView
        android:id="@+id/first_name_card"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_view_height_patient"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:elevation="20dp"
        app:cardCornerRadius="5dp">

        <EditText
            android:id="@+id/first_name"
            android:layout_width="match_parent"
            android:layout_height="@dimen/text_input_height"
            android:background="@color/white"
            android:backgroundTint="@color/white"
            android:drawablePadding="@dimen/drawable_padding"
            android:hint="First Name"
            android:imeOptions="actionNext"
            android:maxLength="35"
            android:inputType="textPersonName"
            android:maxLines="1"
            android:textSize="@dimen/edit_text_size"
            android:padding="10dp"
            />
    </android.support.v7.widget.CardView>



    <android.support.v7.widget.CardView
        android:id="@+id/last_name_card"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_view_height_patient"
        android:layout_marginLeft="10dp"
        android:elevation="20dp"
        app:cardCornerRadius="5dp"
        android:layout_marginBottom="10dp">
        <EditText
            android:id="@+id/last_name"
            android:layout_width="match_parent"
            android:layout_height="@dimen/text_input_height"
            android:background="@color/white"
            android:backgroundTint="@color/white"
            android:drawablePadding="@dimen/drawable_padding"
            android:hint="Last Name"
            android:imeOptions="actionNext"
            android:maxLength="35"
            android:inputType="textPersonName"
            android:maxLines="1"
            android:textSize="@dimen/edit_text_size"
            android:padding="10dp"/>
    </android.support.v7.widget.CardView>



    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_view_height_patient"
        android:layout_marginRight="@dimen/margin_low"
        android:layout_marginLeft="10dp"
        android:layout_gravity="bottom"
        app:cardCornerRadius="5dp"
        android:elevation="20dp"
        android:layout_marginBottom="10dp">
        <Spinner
            android:id="@+id/doctors_list"
            android:layout_width="match_parent"
            android:layout_height="@dimen/spinner_height"
            android:gravity="left">
        </Spinner>
    </android.support.v7.widget.CardView>

    <android.support.v7.widget.CardView
        android:id="@+id/ref_name_card"
        android:layout_width="match_parent"
        android:layout_height="@dimen/card_view_height_patient"
        android:layout_marginLeft="10dp"
        android:elevation="20dp"
        android:layout_marginBottom="10dp"
        app:cardCornerRadius="5dp">
        <EditText
            android:id="@+id/reference"
            android:layout_width="match_parent"
            android:layout_height="@dimen/text_input_height"
            android:background="@color/white"
            android:backgroundTint="@color/white"
            android:drawablePadding="@dimen/drawable_padding"
            android:hint="Reference"
            android:imeOptions="actionNext"
            android:inputType="textPersonName"
            android:maxLength="70"
            android:maxLines="1"
            android:textSize="@dimen/edit_text_size"
            android:padding="10dp"/>
    </android.support.v7.widget.CardView>


        <Button
            android:id="@+id/submit_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="Submit"/>
    </LinearLayout>
</ScrollView>

主要活动。Java

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText firstName;
    private EditText lastName;
    private Spinner selDoc;
    private EditText reference;
    private Button submitBut;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstName = findViewById(R.id.first_name);
        lastName = findViewById(R.id.last_name);
        selDoc = findViewById(R.id.doctors_list);
        reference = findViewById(R.id.reference);
        submitBut = findViewById(R.id.submit_button);
        String[] dicList = getResources().getStringArray(R.array.doctors_list);
        ArrayAdapter<String> docAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dicList);
        selDoc.setAdapter(docAdapter);
        setOnListnerClickedItems();
        setListeners();
    }

    private void setOnListnerClickedItems() {
        submitBut.setOnClickListener(this);
    }
    private void setListeners() {
        lastName.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.submit_button:
                String errorMsg = validate();
                if (errorMsg.toString().isEmpty()) {
                    Toast.makeText(MainActivity.this, "Data Saved Successfully", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    private String validate() {
        if (ComponentUtils.getInputStringFromView(firstName).toString().isEmpty()) {
            return "Please Enter Your First Name";
        }
        else if (!Validator.isValidName(ComponentUtils.getInputStringFromView(firstName))) {
            return "Please Enter Valid First Name";
        }
        else if (ComponentUtils.getInputStringFromView(lastName).toString().isEmpty()) {
            return "Please Enter Your Last Name";
        }
        else  if (!Validator.isValidName(ComponentUtils.getInputStringFromView(lastName))) {
            return "Please Enter Valid Last Name";
        }
        else if (ComponentUtils.getInputStringFromView(selDoc).toString().isEmpty()) {
            return "Please Select Doctor";
        }
        else if (ComponentUtils.getInputStringFromView(reference).toString().isEmpty()) {
            return "Please Enter Reference Name";
        }
        else if (!Validator.isValidReferenceName(ComponentUtils.getInputStringFromView(reference))) {
            return "Please Enter Valid Reference Name";
        }
        else {
            return "";
        }
    }
}

表单中有4个字段,包括firstName、lastName、doctorList和referenceName。我需要验证除INVISIBLE字段之外的所有字段。如代码中所示,3个字段可见,1个字段即lastName不可见。但是,当我输入数据并单击提交按钮时,它显示错误为"请输入姓氏"。如何动态地只验证可见字段?请帮帮我。

为此,您需要检查字段的可见性,并在其中应用验证

例如:

//To check the visibilty of the filed 
if(edittLastName.getVisibility() == View.VISIBLE){
    vaildateField();
} 

验证功能:

private void vaildateField(){
    if(etxLastName.getLength()==0)
        Log.e("Error","Enter last name");
}

添加布尔值

boolean isneedValidation=false;
        else if (isneedValidation &&ComponentUtils.getInputStringFromView(selDoc).toString().isEmpty()) {
                return "Please Enter Your Email ID";
            }

如果isneedValidation为true,则条件将进行检查,否则将跳过每当您想将Edittext更改为可见时,请更改布尔值

这是您问题的确定答案。

private String validate() {
        if (firstName.getVisibility() == View.VISIBLE && ComponentUtils.getInputStringFromView(firstName).toString().isEmpty()) {
            return "Please Enter Your First Name";
        }
        else if (firstName.getVisibility() == View.VISIBLE && !Validator.isValidName(ComponentUtils.getInputStringFromView(firstName))) {
            return "Please Enter Valid First Name";
        }
        else if (lastName.getVisibility() == View.VISIBLE && ComponentUtils.getInputStringFromView(lastName).toString().isEmpty()) {
            return "Please Enter Your Last Name";
        }
        else  if (lastName.getVisibility() == View.VISIBLE && !Validator.isValidName(ComponentUtils.getInputStringFromView(lastName))) {
            return "Please Enter Valid Last Name";
        }
        else if (selDoc.getVisibility() == View.VISIBLE && ComponentUtils.getInputStringFromView(selDoc).toString().isEmpty()) {
            return "Please Select Doctor";
        }
        else if (reference.getVisibility() == View.VISIBLE && ComponentUtils.getInputStringFromView(reference).toString().isEmpty()) {
            return "Please Enter Reference Name";
        }
        else if (reference.getVisibility() == View.VISIBLE && !Validator.isValidReferenceName(ComponentUtils.getInputStringFromView(reference))) {
            return "Please Enter Valid Reference Name";
        }
        else {
            return "";
        }
    }

相关内容

  • 没有找到相关文章

最新更新