xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:textColor="#000000" android:text="小小日记簿" android:textSize="16sp"/> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="200dp" android:layout_below="@+id/tv" android:layout_marginTop="8dp" android:gravity="left|top" android:hint="记录下今天的好心情" android:inputType="textMultiLine"/> <Button android:id="@+id/btn_read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/et_content" android:text="读取"/> <Button android:id="@+id/btn_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/et_content" android:layout_marginRight="6dp" android:layout_toLeftOf="@+id/btn_read" android:text="保存"/> <TextView android:id="@+id/show_Text1" android:layout_width="match_parent" android:layout_height="70sp" android:layout_below="@+id/btn_save" android:layout_alignParentStart="true" android:layout_marginTop="41dp" /> <TextView android:id="@+id/show_Text2" android:layout_width="match_parent" android:layout_height="70sp" android:layout_below="@+id/show_Text1" android:layout_alignParentStart="true" android:layout_marginTop="41dp" /> <TextView android:id="@+id/show_Text3" android:layout_width="match_parent" android:layout_height="70sp" android:layout_below="@+id/show_Text2" android:layout_alignParentStart="true" android:layout_marginTop="41dp" /> </RelativeLayout>
public class MainActivity extends AppCompatActivity { private EditText etContent; private Button btnSave; private Button btnRead; private Calendar calendar; private TextView showText1; private TextView showText2; private TextView showText3; private static int sum=1; private static int t=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etContent=(EditText)findViewById(R.id.et_content); btnSave=(Button)findViewById(R.id.btn_save); btnRead=(Button)findViewById(R.id.btn_read); showText1= (TextView)findViewById(R.id.show_Text1); showText2= (TextView)findViewById(R.id.show_Text2); showText3= (TextView)findViewById(R.id.show_Text3); final Calendar calender = Calendar.getInstance(); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v){ String content=etContent.getText().toString().trim(); if(!TextUtils.isEmpty(content)){ String fileName=""+calender.get(Calendar.YEAR)+"-"+(calender.get(Calendar.MONTH)+1)+"-"+ calender.get(Calendar.DAY_OF_MONTH)+"-"+calender.get(Calendar.HOUR_OF_DAY)+"-"+ calender.get(Calendar.MINUTE)+"-"+calender.get(Calendar.SECOND)+ "("+sum+")"+".txt";sum++; FileOutputStream fos=null; BufferedWriter bw=null; try{ fos=openFileOutput(fileName,MODE_APPEND); bw=new BufferedWriter(new OutputStreamWriter(fos)); bw.write(content); bw.flush(); Toast.makeText(MainActivity.this, "日记保存成功", Toast.LENGTH_SHORT).show(); }catch (Exception e){ e.printStackTrace(); Toast.makeText(MainActivity.this,"日记保存失败",Toast.LENGTH_SHORT).show(); }finally { if(bw!=null){ try{ bw.close(); }catch (IOException e){ e.printStackTrace(); } } } }else{ Toast.makeText(MainActivity.this, "请输入", Toast.LENGTH_SHORT).show(); } } } ); btnRead.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ String fileName=""+calender.get(Calendar.YEAR)+"-"+(calender.get(Calendar.MONTH)+1)+"-"+ calender.get(Calendar.DAY_OF_MONTH)+"-"+calender.get(Calendar.HOUR_OF_DAY)+"-"+ calender.get(Calendar.MINUTE)+"-"+calender.get(Calendar.SECOND)+"("+t+")"+".txt"; t++; FileInputStream fis=null; BufferedReader br=null; try{ fis=openFileInput(fileName); br=new BufferedReader(new InputStreamReader(fis)); String line=null; StringBuffer buffer=new StringBuffer(); while ((line=br.readLine())!=null){ buffer.append(line).append("\r\n"); } if(!TextUtils.isEmpty(buffer.toString())){ if(t%3==0)showText2.setText(buffer.toString()); if(t%3==1)showText3.setText(buffer.toString()); if(t%3==2)showText1.setText(buffer.toString()); } }catch (Exception e){ e.printStackTrace(); Toast.makeText(MainActivity.this, "今天还没写日记", Toast.LENGTH_SHORT).show(); }finally { if(br!=null){ try{ br.close(); }catch (IOException e){ e.printStackTrace(); } } } } }); }
}