2016-08-27 2 views
1

Программа работает нормально, но я не могу понять, почему она будет читать и писать только одну строку. Он должен иметь возможность писать несколько строк и читать их в текстовом виде. У меня есть его установки, так что, когда пользователь щелкает добавить его автоматически следует читать в 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> 
+0

редактировать свой вопрос и показать layout.activity_main.xml файл –

ответ

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"); 
    } 
+0

добавил, что, но он по-прежнему заменяет текущую строку с новой строки. Не добавляет еще одну строку –

+0

Я обновил свой ответ, пожалуйста, проверьте это и ответьте мне –

+0

Этот код не работает. Однако он дал мне идею stringBuffer.append (Log.getText(). ToString() + action + "\ n"); это отлично работает –

Смежные вопросы