Android traversal controls
Overview
When we log in or register to submit what data we need to fill our personal information, so we need to determine our fields when they are entered.
How do we traverse the Android interface controls
According to international practice, we look at the 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;
}
}
}
});
}
}
The code is the code that we realize the full realization of interface controls the traverse.
Then we have to explain:
We first determine a large main layout.
Then get his child controls. And to acquire through our use his Count.
Use instanceof keyword is not what you want to judge the controls.
His final judgment text length is not zero can be achieved is determined whether an operation of the entire field.