EditText getHint() returns null when using design support library

后端 未结 3 1090
情歌与酒
情歌与酒 2020-12-17 17:19

When using EditText in combination with Design lib\'s (ver 22.2.1) TextInputLayout getting hint programmatically returns null.

I\'m trying to append asterisk \'*\'

相关标签:
3条回答
  • 2020-12-17 17:52

    Actually the hint moves to the parent view TextInputLayout that surrounds the EditText view:

    You can get the hint like this:

    android.support.design.widget.TextInputLayout parent = (android.support.design.widget.TextInputLayout) yourEditText.getParent();
    String hint = parent.getHint().toString();
    

    And if you want to add * make it like this:

    parent.setHint(parent.getHint() + "*");
    

    Happy codding! :)

    0 讨论(0)
  • 2020-12-17 17:54

    This is fixed in design support 23.0.0 but the project has to be compiled for api 23.

    build.gradle

    android {
        compileSdkVersion 23
        buildToolsVersion '23'
        ....
    }
    dependencies{
        compile 'com.android.support:appcompat-v7:23.0.0'
    ...
    }
    

    Setting the hint on the EditText like before:

    <android.support.design.widget.TextInputLayout
        android:id="@+id/inputField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hello_world"
            android:inputType="text"/>
    

    Now adding an extra character to the hint:

    TextInputLayout inputField = (TextInputLayout) findViewById(R.id.inputLayout);
    String hint = String.format("%s *", inputField.getHint());
    inputField.setHint(hint);
    
    0 讨论(0)
  • 2020-12-17 18:03

    Hint is linked with its's parent layout

    ((TextInputLayout)view.getParent()).getHint()

    0 讨论(0)
提交回复
热议问题