【问题标题】:TextView in ScrollView not updatingScrollView 中的 TextView 未更新
【发布时间】:2016-09-26 14:40:45
【问题描述】:

我在 scrollView 中创建 textView。每次我在 textView 中设置文本时,文本不会更新,但是当我打开键盘然后关闭它时,文本会更新.. 谷歌搜索后,我得到的解决方案是调用 textView.invalidate() 和 textView.requestLayout()。但我很好奇为什么没有调用 invalidate 和 requestLayout 就不会更新? scrollView 是否有一些'special' 所以我需要调用 invalidate 和 requestLayout?

这里是代码

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.zihadrizkyef.belajarinternalstorage.MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:orientation="vertical">

        <EditText
            android:id="@+id/etWrite"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:gravity="top"
            android:hint="write text here"/>
        <View android:id="@+id/separator1"
              android:layout_width="match_parent"
              android:layout_height="1px"
              android:layout_marginTop="20dp"
              android:layout_marginBottom="20dp"
              android:background="#aaa"/>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
            <TextView
                android:id="@+id/tvRead"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="(no text)"/>
        </ScrollView>
        <View android:id="@+id/separator2"
              android:layout_width="match_parent"
              android:layout_height="1px"
              android:layout_marginTop="20dp"
              android:layout_marginBottom="20dp"
              android:background="#aaa"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="2"
        android:gravity="center">

        <Button
            android:id="@+id/btnSave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:onClick="onClick"
            android:text="save"/>
        <Button
            android:id="@+id/btnLoad"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:onClick="onClick"
            android:text="load"/>
    </LinearLayout>
</LinearLayout>

MainActivity.java

package com.zihadrizkyef.belajarinternalstorage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private final String FNAME = "mydata";
    EditText etWrite;
    TextView tvRead;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etWrite = (EditText)findViewById(R.id.etWrite);
        tvRead = (TextView)findViewById(R.id.tvRead);
    }


    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btnSave:
                String data = etWrite.getText().toString();
                try {
                    FileOutputStream fOut = openFileOutput(FNAME, MODE_PRIVATE);
                    fOut.write(data.getBytes());
                    fOut.close();
                    Toast.makeText(MainActivity.this, "File saved", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            break;
            case R.id.btnLoad:
                int cRead;
                String read="";

                try {
                    FileInputStream fIn = openFileInput(FNAME);
                    while((cRead=fIn.read())!=-1) {
                        read += Character.toString((char)cRead);
                    }
                    tvRead.setText(read);
                    tvRead.invalidate();
                    tvRead.requestLayout();
                    Toast.makeText(MainActivity.this, "File loaded", Toast.LENGTH_SHORT).show();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            break;
        }
    }
}

github:https://github.com/zihadrizkyef/FileWriteRead_InternalStorage

【问题讨论】:

  • 文本视图不会不断刷新以节省性能,通过打开和关闭您重新加载它或通过使布局无效并请求您也重新加载它。当您使用诸如 android 图库之类的东西时也是如此。我希望这有帮助:)
  • 你能详细说明一下保存和加载文本的场景吗?我要求它是因为我使用了带有 requestLayoutinvalidate 注释的代码,并且它按预期工作,因为在 setText 方法的末尾调用了这两个方法。最后,添加您正在测试的设备和 android 版本,因为问题可能来自那里
  • @Just_someone 谢谢兄弟,但你能证明吗?这样的文章或滚动视图源代码:D
  • @ZihadRizkyEdwinFikri 我找不到这样的文章,但我确实有 3 部手机整晚都在运行刷新测试,这让我得出结论,邮件布局每 2-5 秒刷新一次(取决于电话)或更新。但在二级布局中,它只会每隔几分钟重新加载一次,或者在某些手机上甚至不会重新加载。祝你的项目好运。
  • @ZihadRizkyEdwinFikri 我按照你说的尝试了,一切都对我有用。一旦我点击保存按钮,就会显示带有保存消息的Toast,当我点击加载按钮时,TextView 会更新并显示Toast。您是否有其他代码可能会导致此问题,或者所有内容都在 github 上?

标签: java android


【解决方案1】:

您可以尝试使用 AsyncTask 更新值。由于 onPostExecute() 方法在 UI 线程中运行,它可以动态地进行任何类型的 UI 更新。更多信息请参考:https://developer.android.com/reference/android/os/AsyncTask.html

【讨论】:

    【解决方案2】:

    您已将 ScrollView 的高度和 TextView 的高度设置为“0”,并且没有为 TextView 设置任何文本,因此您必须使视图无效才能再次测量其布局参数。

    您可以使用此链接: Making TextView scrollable on Android

    【讨论】:

    • 将高度设置为0 并不意味着实际的视图高度将为0。如果您稍微注意一下代码,您会发现他使用weight 属性使视图填充其父视图。发帖前请阅读代码
    【解决方案3】:

    很明显, 每当您更改控件的数据时,它都不会反映在视图中,除非该控件或其父控件或视图无效。

    TextView 的文档中也提到过 https://developer.android.com/reference/android/widget/TextView.html#setText(char[], int, int)

    【讨论】:

    • 这不是文档的意思(另外,这不是 OP 使用的方法)。文档说,如果你传递一个char[],然后对 char[] 的内容进行后续修改 ,TextView 就无法知道它。但是显然,如果当你调用setText 时,修改会反映在视图上*。 (除非特殊情况)
    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多