2016-10-04 23 views
0

當註冊時用戶輸入無效emailmobile時,我將錯誤設置爲EditText在InputTextLayout中將錯誤設置爲EditText時,只有錯誤圖標可見

請檢查下面的代碼:

<android.support.design.widget.TextInputLayout 
     android:id="@+id/txt_input_mobile_number" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <EditText 
      android:id="@+id/input_mobile_number" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="Mobile number" 
      android:maxLength="10" 
      android:inputType="number" 
      android:maxLines="1"/> 

    </android.support.design.widget.TextInputLayout> 

MyFragment.java可實現以下OnKeyListener

@Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
       (keyCode == KeyEvent.KEYCODE_ENTER)) { 
      // perform validation of data here 
      switch (v.getId()) { 

       case R.id.input_mobile_number: 
        EditText editMobile = (EditText)getActivity().findViewById(R.id.input_mobile_number); 
        Editable phone = binding.inputMobileNumber.getText(); 
        if (phone != null && isValidMobile(phone.toString())) { 
         editMobile.setError("Please enter valid mobile number"); 
         editMobile.requestFocus(); 
        } 
        break; 
      } 
      return true; 
     } 
     return false; 
    } 

下面的截圖顯示,只有錯誤圖標就像是另外一個可見不是錯誤對話框:

與只有符號 enter image description here

與錯誤提示

enter image description here

+0

很難得到哪個版本是你想要的,以及你想要什麼。所以我們有:*當用戶在註冊時輸入無效的電子郵件或手機時,我將錯誤設置爲EditText。 下面的屏幕截圖顯示,只有錯誤圖標可見不是錯誤框像其他* *和一些代碼轉儲**沒有顯示什麼問題** – Selvin

+1

嘗試設置錯誤到'InputTextLayout'你設置爲'EditText'這就是爲什麼.. – Ironman

回答

2

您應該使用TextInputLayout而不是EditText要求

TextInputLayout tilObj = (TextInputLayout) findViewById(R.id.txt_input_mobile_number); 
tilObj .setErrorEnabled(true); 
tilObj .setError("Please enter valid mobile number"); 
1

想通了!!

當我們使用TextInputLayout我們需要設置錯誤不EditTextTextInputLayout本身

所以我改變了我的代碼如下:

@Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
        (keyCode == KeyEvent.KEYCODE_ENTER)) { 
       // perform validation of data here 
       switch (v.getId()) { 

        case R.id.input_mobile_number: 
         // check it is TextInputLayout not EditText 
         TextInputLayout editMobile = (EditText)getActivity().findViewById(R.id.txt_input_mobile_number); 
         Editable phone = binding.inputMobileNumber.getText(); 
         if (phone != null && isValidMobile(phone.toString())) { 
          editMobile.setError("Please enter valid mobile number"); 
          editMobile.requestFocus(); 
         } 
         break; 
       } 
       return true; 
      } 
      return false; 
     } 
0

也許這兩個TextInputLayout之間的空間很小,並且您看不到錯誤消息,請嘗試在它們之間放置更多的邊距。