Ich TextView in scrollView erstellen. Jedes Mal, wenn ich texttext setze, wird der Text nicht aktualisiert, aber wenn ich die Tastatur öffne, schließe sie dann, der aktualisierte Text .. nach googlen, ich habe die Lösung ist Aufruf textView.invalidate() und textView.requestLayout(). Aber ich bin currious, warum es nicht ohne Anruf invalidate und requestLayout aktualisiert? Ist scrollView etwas 'spezielle' so muss ich Invalidate und requestLayout aufrufen?TextView in ScrollView nicht aktualisieren
hier ist der Code
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
die Textview tut refresh ständig performence zu sparen, durch Öffnen und Schließen Sie es neu zu laden oder ungültig zu machen und die Anforderung des Layouts Sie es auch reaload. Das ist das Gleiche, wenn Sie Dinge wie die Android-Galerie verwenden. Ich hoffe, das hat geholfen :) –
Können Sie das Szenario des Speicherns und Ladens des Textes ein wenig genauer erläutern? Ich frage danach, weil ich Ihren Code mit dem 'requestLayout' und' invalidate' kommentiert habe, und es funktioniert wie erwartet, da diese beiden Methoden am Ende der 'setText' Methode aufgerufen werden. Als letzte Sache, fügen Sie das Gerät und Android-Version, die Sie testen, da das Problem von dort sein könnte –
@Just_someone Dank Bro, aber können Sie es beweisen? so ein Artikel oder scrollview Quellcode: D – zihadrizkyef