ScrollView中的TextView无法更新

时间:2016-09-26 14:40:45

标签: java android

我在scrollView中创建textView。每次我setText文本视图,文本不更新,但当我打开键盘然后关闭它,文本更新.. 谷歌搜索后,我得到的解决方案是调用textView.invalidate()和textView.requestLayout()。但我很奇怪为什么没有调用invalidate和requestLayout它没有更新? scrollView有一些' 特殊'所以我需要调用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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:-1)

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

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