2014-07-02 42 views
2

我使用setError()來顯示驗證錯誤。在一種情況下,該字段靠近屏幕的左側,當出現錯誤消息時,它會直觀地指向錯誤的EditText字段 - 而不是指向顯示1234的字段,它應該指向空白字段左邊,看附件截圖。有沒有人知道這個解決方案?謝謝!Android setError():視覺上指向錯誤字段的錯誤消息

截圖是在Galaxy 3手機上運行的Android 4.4.2。

enter image description here

回答

0

我得到了一個解決方法,添加\ n格式化消息,並減少各線的寬度:

text1.setError("this field must\nbe 1 character\nin length"); 
0

這是你的問題的解決方案.. 試試這個..

EditText edt1=(EditText) findViewById(R.id.editText2); 
    EditText edt2=(EditText) findViewById(R.id.editText3); 
    EditText edt3=(EditText) findViewById(R.id.editText1); 
    // Reset errors. 
    edt1.setError(null); 
    edt2.setError(null); 
    edt3.setError(null); 

    String txt3 = edt3.getText().toString().trim(); 
    String txt1 = edt1.getText().toString().trim(); 
    String txt2=edt2.getText().toString().trim(); 
    boolean cancel = false; 
    View focusView = null; 
    if (TextUtils.isEmpty(txt3)) { 
     edt3.setError(getString(R.string.error_field_required)); 
     focusView = edt3; 
     cancel = true; 
    } 
    if (TextUtils.isEmpty(txt2)) { 
     edt2.setError(getString(R.string.error_field_required)); 
     focusView = edt2; 
     cancel = true; 
    } 

    if (TextUtils.isEmpty(txt1)) { 
       edt1.setError(getString(R.string.error_field_required)); 
       focusView = edt1; 
       cancel = true; 
      } 



      if (cancel) { 

       focusView.requestFocus(); 
      } else { 

           //do the operations you want do here 

      } 
+0

您是否試過這段代碼? – Darish