程序运行正常,但是我无法弄清楚为什么它只会读写一行。它应该能够写多行并将其读入textview。我进行了设置,以便当用户单击时将其自动添加到textview中
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);
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mbl404.phoenix.edu.week2appgk5343.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:hint="Please Enter Type of Workout"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignEnd="@+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="Please Enter Number of Miles"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:hint="Please Enter Date"
android:layout_below="@+id/editText2"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="Add"
/>
<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:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
最佳答案
将此添加到您的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");
}