小小日记簿

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();
                        }
                    }
                }
            }
        });
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值