Comparing two editTexts in android

前端 未结 6 1534
既然无缘
既然无缘 2020-12-22 05:01

I am learning android I tried following codeline but it\'s giving me error please give me suggestions, that how can I compare two edittext\'s text.



        
6条回答
  •  北海茫月
    2020-12-22 05:38

    Here's a solution that doesn't violate the DRY principle:

    private static boolean allContain(final String value, 
                                      final EditText... editTexts)
    {
    
        for (EditText editText : editTexts) {
            final String text = editText.getText().toString();
            if (!text.equals(value)) {
                return false;
            }
        }
        return true;
    }
    

    You can use it as follows:

    if (allContain("X", edt1, edt2, edt3, edt4)) {
        // All EditTexts contain 'X'
    }
    

提交回复
热议问题