Android Instance for determining whether all fields have been entered

  • 2021-11-24 03:00:47
  • OfStack

Android Traversal Control

Overview

We need to fill in our personal information when we log in or register to submit any data, so we need to judge when our fields are entered.

How does Android traverse the controls in our interface

According to international practice, let's look at 1 source code:


package com.android.gesture.study_01_08;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Android_2Activity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_android_2);

  findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    ConstraintLayout root = findViewById(R.id.android_root);
    for (int i = 0; i < root.getChildCount(); i++) {
     View view = root.getChildAt(i);

     if (view instanceof EditText && ((EditText) view).getText().length() == 0) {
      Toast.makeText(Android_2Activity.this, "Sorry Please fill in all the information", Toast.LENGTH_SHORT).show();
      return;
     }
    }
   }
  });


 }
}

These codes are all the codes that realize our traversal of the controls in the interface.

Then let's explain 1:

Let's first determine a large main layout.

Then get its child controls. And get it through our Count.

Use the instanceof keyword to determine whether it is the control you want.

Finally, judging whether his text length is 0 can realize the operation of judging whether all fields are input.

Additional knowledge: Android-Determining the current time period

Idea: Get the minutes of the current time relative to the current day, and then compare it with the time after the time period (converted into minutes) in chronological order (such as 8:00-9:00, compared with 9:00).

Partial code:

Current time


Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int CurrentTimeTotalMinute = hour * 60 + minute;

Time period: String time= "8:00-9:00"


 String[] two = time.split("-");
 String[] second = two[1].split(":");
 int secondTotalMinute = Integer.valueOf(second[0]) * 60 + Integer.valueOf(second[1]);

Related articles: