效果图
工程目录:
AddActivity代码:
package cn.fanting.notpadnew;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class AddActivity extends AppCompatActivity {
private EditText editText;//正文
@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
//将系统自带的标题栏隐藏掉
ActionBar actionbar = getSupportActionBar();
if (actionbar != null){
actionbar.hide();
}
//关联布局控件
editText = findViewById(R.id.add_text);
TextView textView = findViewById(R.id.add_time);
textView.setText(timeStamp1()+" "+timeStamp2()+" ");//将时间填充到
//自动弹出软键盘
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
java.util.Timer timer = new java.util.Timer();
timer.schedule(new java.util.TimerTask(){
public void run(){
android.view.inputmethod.InputMethodManager inputManager =(android.view.inputmethod.InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
assert inputManager != null;
inputManager.showSoftInput(editText, 0);
}
},300);
//注册"分享"按钮的监听事件
Button button_share = findViewById(R.id.add_share);
button_share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_share:
shareMsg();//分享
break;
}
}
});
}
@Override//销毁页面前保存文本
protected void onDestroy() {
super.onDestroy();
String inputText = editText.getText().toString();//将获取到的EditText内的文本转化为字符串
if (!TextUtils.isEmpty(inputText)) {
if (!TextUtils.isEmpty(inputText)) {//文本不为空
save(inputText);//存储到文件指定文件中
} else {
save(inputText);//存储到文件指定文件中
}
}
}
//常用方法封装
//时间戳
public static String timeStamp1(){
Date time = new Date();
//SimpleDateFormat format1 = new SimpleDateFormat( "yy年MM月dd日 HH:mm:ss" );//定义时间格式
SimpleDateFormat format1 = new SimpleDateFormat( "yy-MM-dd" );//定义时间格式
return format1.format(time);
}
public static String timeStamp2(){
Date time = new Date();
SimpleDateFormat format2 = new SimpleDateFormat( "HH:mm:ss" );//定义时间格式
return format2.format(time);
}
//存储标题、文本
private void save(String text) {
BufferedWriter writertext = null;
String saveFileName;
if (text.length() >= 9) {
saveFileName = timeStamp1()+" "+timeStamp2()+" "+text.substring(0, 9);
}else {
saveFileName = timeStamp1()+" "+timeStamp2()+" "+ text;
}
try {
FileOutputStream outtext = openFileOutput(saveFileName, Context.MODE_PRIVATE);
writertext = new BufferedWriter(new OutputStreamWriter(outtext));
writertext.write(text);
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (writertext != null){
writertext.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
}
//分享方法
public void shareMsg() {
editText.getText(); //获取文本
String text = editText.getText().toString();//转化为字符串
if(!text.equals("")) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain"); // 纯文本
intent.putExtra(Intent.EXTRA_TEXT, text);//分享内容
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "分享文本"));//弹框标题
}else if (text.equals("")){
Toast.makeText(getApplicationContext(), "没有内容", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(getApplicationContext(), "分享失败", Toast.LENGTH_LONG).show();
}
}
}
Book代码:
package cn.fanting.notpadnew;
public class Book {
private String name;
private int imageId;
Book(String name, int imageId){
this.name = name;
this.imageId = imageId;
}
public String getName(){
return name;
}
public int getImageId(){
return imageId;
}
}
BookAdapter代码:
package cn.fanting.notpadnew;
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.ViewHolder> {
private List<Book> mBookList;
private MainActivity mainActivity;//引用
static class ViewHolder extends RecyclerView.ViewHolder{//定义内部类:ViewHolder,继承自RecyclerView.ViewHolder
ImageView bookImage;
TextView bookName;
View bookView;//点击
ViewHolder(View view){//ViewHolder的构造函数中传入参数view
super(view);
bookImage = view.findViewById(R.id.book_image);//通过findViewById方法获取布局中的ImageView和TextView
bookName = view.findViewById(R.id.book_name);
bookView = view;//点击
}
}
BookAdapter(MainActivity mainActivity, List<Book> bookList){//将展示的数据源传给bookList
this.mainActivity = mainActivity;
mBookList = bookList;//赋值给一个全局变量mBookList,后续操作都在mBookList数据源进行
}
//因为BookAdapter是继承自RecyclerV