9个方形EditText自动填值校验弹窗功能失效求助
Hey there! Let's work through getting those 9 square EditTexts to automatically trigger a popup once every single one is filled in. I’ve got a step-by-step solution that should fix whatever’s going wrong with your current code.
The core idea is to add a text change listener to each EditText, and every time the input changes, we check if all fields have content. We’ll also add a flag to avoid the popup repeating unnecessarily.
Step 1: Prep Your Layout
First, make sure each of your 9 EditTexts has a unique ID in your XML layout (like et_input1 through et_input9). This makes it easy to reference them in your activity code.
Step 2: Implement the Text Watcher (Kotlin Example)
We’ll create a single TextWatcher and attach it to all EditTexts to avoid repetitive code. The flag hasShownPopup prevents the dialog from popping up multiple times as the user types.
class MainActivity : AppCompatActivity() { private var hasShownPopup = false private lateinit var editTextList: List<EditText> override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Initialize all 9 EditTexts in a list editTextList = listOf( findViewById(R.id.et_input1), findViewById(R.id.et_input2), findViewById(R.id.et_input3), findViewById(R.id.et_input4), findViewById(R.id.et_input5), findViewById(R.id.et_input6), findViewById(R.id.et_input7), findViewById(R.id.et_input8), findViewById(R.id.et_input9) ) // Create a shared TextWatcher for all fields val textChangeListener = object : TextWatcher { override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {} override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { // Check if all fields are filled every time text changes checkAllFieldsFilled() } override fun afterTextChanged(s: Editable?) {} } // Attach the listener to each EditText editTextList.forEach { it.addTextChangedListener(textChangeListener) } } private fun checkAllFieldsFilled() { // Verify every EditText has non-empty, non-whitespace content val allFilled = editTextList.all { it.text.toString().trim().isNotEmpty() } if (allFilled && !hasShownPopup) { // Show your popup dialog (AlertDialog example here) AlertDialog.Builder(this) .setTitle("Success!") .setMessage("All fields have been filled out completely.") .setPositiveButton("Got it") { dialog, _ -> dialog.dismiss() // If you want the popup to reappear if fields are cleared and refilled, uncomment below // hasShownPopup = false } .show() hasShownPopup = true } else if (!allFilled) { // Reset the flag if any field is cleared hasShownPopup = false } } }
Step 3: Java Version (If You’re Using Java)
Here’s the equivalent code for Java developers:
public class MainActivity extends AppCompatActivity { private boolean hasShownPopup = false; private List<EditText> editTextList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the list of EditTexts editTextList = Arrays.asList( findViewById(R.id.et_input1), findViewById(R.id.et_input2), findViewById(R.id.et_input3), findViewById(R.id.et_input4), findViewById(R.id.et_input5), findViewById(R.id.et_input6), findViewById(R.id.et_input7), findViewById(R.id.et_input8), findViewById(R.id.et_input9) ); TextWatcher textChangeListener = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { checkAllFieldsFilled(); } @Override public void afterTextChanged(Editable s) {} }; // Attach listener to each EditText for (EditText et : editTextList) { et.addTextChangedListener(textChangeListener); } } private void checkAllFieldsFilled() { boolean allFilled = true; for (EditText et : editTextList) { if (et.getText().toString().trim().isEmpty()) { allFilled = false; break; } } if (allFilled && !hasShownPopup) { new AlertDialog.Builder(this) .setTitle("Success!") .setMessage("All fields have been filled out completely.") .setPositiveButton("Got it", (dialog, which) -> { dialog.dismiss(); // Uncomment below to allow popup to reappear later // hasShownPopup = false; }) .show(); hasShownPopup = true; } else if (!allFilled) { hasShownPopup = false; } } }
Common Issues to Check
If your original code wasn’t working, it might be due to:
- Missing or incorrect EditText IDs (causing a
NullPointerException) - Forgetting to attach the text change listener to all fields
- Not trimming whitespace (so empty spaces were being counted as valid input)
- No flag to prevent repeated popups (leading to multiple dialogs appearing)
Compare your code to the examples above, and you should get it working smoothly!
内容的提问来源于stack exchange,提问作者jamel

