只会读写一行

时间:2016-08-27 17:02:16

标签: java android input local-storage fileoutputstream

程序运行正常然而我无法弄清楚为什么它只会读写一行。它应该能够写出多行并将它们读入textview。我进行了设置,以便当用户点击时自动添加它应该读入textview

result_sm

}

public class MainActivity extends AppCompatActivity {
EditText Activity, Miles, Date;
TextView Log;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Activity = (EditText)(findViewById(R.id.editText));
    Miles = (EditText)(findViewById(R.id.editText2));
    Date = (EditText)(findViewById(R.id.editText3));
    Log = (TextView)(findViewById(R.id.textView));
}
public void Add(View view)
{
    String Myactivity = Activity.getText().toString() + "\t" +   Miles.getText().toString() + "\t" + Date.getText().toString();
    try {
        FileOutputStream fileOutputStream =  openFileOutput("myActivities.txt", MODE_PRIVATE);
        fileOutputStream.write(Myactivity.getBytes());
        fileOutputStream.close();
        Toast.makeText(getApplicationContext(), "Activty Added", Toast.LENGTH_LONG);
        FileInputStream fileInputStream = openFileInput("myActivities.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer stringBuffer = new StringBuffer();
        String action;
        while((action = bufferedReader.readLine())!= null)
        {
            stringBuffer.append(action + "\n");
        }
        Log.setText(stringBuffer.toString());

    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_LONG);
    } catch (IOException e)
    {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_LONG);
    }


}

1 个答案:

答案 0 :(得分:0)

textview xml

中添加此内容
   android:maxLines="100"

在您的代码中

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView"
    android:layout_below="@+id/button"
    android:maxLines="100" <!-- add android:maxLines to specify the number of lines in textview -->
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true" />

<强>更新

    while((action = bufferedReader.readLine())!= null)
    {
        Log.setText(Log.getText() + action + "\n");
    }