【问题标题】:setText not working for a Custom EdittextsetText 不适用于自定义 Edittext
【发布时间】:2016-04-29 04:43:27
【问题描述】:

我正在使用从 github 找到的一个不错的 Material Design 编辑文本:https://github.com/rengwuxian/MaterialEditText

并将依赖部分更新为:

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

edittext xml定义如下:

    <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"/>

所以我希望通过浏览文件系统以编程方式填充编辑文本的值。因此,edittext 是可点击的,但不可聚焦,因此用户无法编辑接收到的文件的路径。现在这个 (device_file_location_value) 编辑文本由从其他片段(实现文件浏览器的片段)接收的值更新,并在其片段的onCreateView 中更新如下:

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

不知何故,这并没有更新 UI 上的文本。我也尝试过使用filePathValue.invalidate(),但这也没有帮助。

任何帮助表示赞赏。

【问题讨论】:

  • @MarkKeen 没用。但 stefno 的解决方案成功了。

标签: android android-edittext settext


【解决方案1】:

这样试试。

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

【讨论】:

  • 你能否解释一下为什么这会起作用。同时我会尝试一下。谢谢。
  • 因为你不能在主线程上修改GUI,所以这样你就创建了一个新线程来完成你的目标。
  • 你能推荐一些关于这个的阅读吗?我对 android 和学习东西还很陌生。
  • 你能不能也看看这个问题:stackoverflow.com/questions/34949634/….
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多