setText not working for a Custom Edittext

2019-07-14 05:50发布

问题:

I am using a nice Material design edit text which I found from github: https://github.com/rengwuxian/MaterialEditText

and updated the dependency section as :

dependencies {
   compile 'com.rengwuxian.materialedittext:library:2.0.3'
}

The edittext xml is defined as follows:

    <com.rengwuxian.materialedittext.MaterialEditText
        android:id="@+id/device_file_location_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_file_location"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:enabled="true"
        android:focusable="false"
        android:clickable="true"
        app:met_floatingLabel="normal"
        app:met_floatingLabelText="@string/file_location"
        app:met_singleLineEllipsis="true"
        app:met_baseColor="#FCFBE3"
        app:met_primaryColor="#FCFBE3"/>

So I am expecting to fill the value of the edit text programatically by browsing the file system. So the edittext is clickable but not focusable and hence user cannot edit the path of file received. Now this (device_file_location_value) edit text is updated by a value received from other fragment(the fragment that implements the file browser) and is updated as follows in the onCreateView of its fragment:

        Bundle bundle = getArguments();
        String filePath = bundle.getString(FileBrowserFragment.PARAM_FILE_PATH);
        if (filePath != null) {
            filePathValue.setText(filePath);
            bundle.remove(FileBrowserFragment.PARAM_FILE_PATH);
        }

Somehow this is not updating the text on the UI. I have also tried using filePathValue.invalidate() but that didn't help either.

Any help appreciated.

回答1:

Try in this way.

filePathValue.post(new Runnable() {
                @Override
                public void run() {
                    filePathValue.setText(filePath);
                }
            })