在InputTextLayout

时间:2016-10-04 12:29:53

标签: android android-edittext android-textinputlayout

当用户在注册时输入无效emailmobile时,我将错误设置为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.javaOnKeyListener下面实现:

 @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

3 个答案:

答案 0 :(得分:2)

您的要求应使用TextInputLayout代替EditText

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

答案 1 :(得分:1)

想通了!

当我们使用TextInputLayout时,我们需要将错误设置为EditText而不是TextInputLayout本身

所以我改变了我的代码如下:

  @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;
        }

答案 2 :(得分:0)

也许这两个TextInputLayout之间的空间很小,你看不到错误信息,试着在它们之间留出更多的余量。