2016-09-26 193 views
3

我在scrollView中創建了textView。每當我setText文本視圖,文本沒有更新,但當我打開鍵盤然後關閉它,文本更新..谷歌搜索後,我得到的解決方案是調用textView.invalidate()和textView.requestLayout()。 。但我很currious爲什麼它沒有更新沒有調用invalidate和requestLayout? scrollView有一些'特殊'所以我需要調用invalidate和requestLayout?ScrollView中的TextView不更新

這裏是代碼

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

+1

TextView的犯規不斷刷新保存性能自動,通過打開和關閉你重新加載或無效,並要求你也reaload它的佈局。當你使用android庫之類的東西時,這是一樣的。我希望這有助於:) –

+0

你能詳細闡述一下保存和加載文本的場景嗎?我在問,因爲我用'requestLayout'和'invalidate'註釋了你的代碼,並且它按預期工作,因爲這兩個方法在'setText'方法的末尾被調用。作爲最後一件事,添加您正在測試的設備和Android版本,因爲問題可能來自 –

+0

@Just_someone感謝broh,但是你能證明它嗎?這樣的文章或滾動視圖源代碼:D – zihadrizkyef

回答

-1

其顯然, 每當您更改控件的數據時,它都不會反映到視圖中,除非控件或其父控件或視圖無效。

其在TextView的 https://developer.android.com/reference/android/widget/TextView.html#setText(char[],INT,INT)

+1

這不是該文檔的含義(同樣,這不是OP使用的方法)。該文檔說,如果你傳遞一個'char []',並對char [] *的內容進行後續修改,那麼TextView無法知道它。但*明顯*,如果你調用'setText'時,修改反映在視圖*上。 (除非特殊情況) – njzk2

0

您已將ScrollView的高度和TextView的高度設置爲「0」,並且沒有爲TextView設置任何文本,因此您必須使視圖無效才能測量其佈局參數再次。

你可以使用這個鏈接: Making TextView scrollable on Android

+0

將高度設置爲「0」並不意味着實際視圖高度爲「0」。如果您稍微注意一下代碼,您會發現他使用'weight'屬性來使視圖填充其父項。發佈內容之前請閱讀代碼 –