社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
时间戳的编写包括记事本编辑区域的时间展示和记事本列表的时间展示
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.v("时间:",sdf.format(date));
tv_now.setText(sdf.format(date));
private void saveNote(){
NoteDataBaseHelper dbHelper = MainActivity.getDbHelper();
ContentValues values = new ContentValues();
values.put(Note.title,et_title.getText().toString());
values.put(Note.content,et_content.getText().toString());
values.put(Note.time,tv_now.getText().toString());
if (insertFlag){
Note.insertNote(dbHelper,values);
}else{
Note.updateNote(dbHelper,Integer.parseInt(currentNote.getId()),values);
}
}
<TextView
android:id="@+id/tv_now"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="#A5BECE"
android:textAlignment="textEnd"
android:gravity="end|center_vertical"
android:paddingRight="8dp"/>
private void initView(){
btn_save = findViewById(R.id.btn_save);
btn_return = findViewById(R.id.btn_return);
tv_now = findViewById(R.id.tv_now);
et_content = findViewById(R.id.edit_content);
et_title = findViewById(R.id.edit_title);
//以下为测试代码
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Log.v("时间:",sdf.format(date));
tv_now.setText(sdf.format(date));
}
//从数据库中读取所有笔记 封装成List<NoteInfo>
private void getNoteList(){
noteList.clear();
Cursor allNotes = Note.getAllNotes(dbHelper);
for (allNotes.moveToFirst(); !allNotes.isAfterLast(); allNotes.moveToNext()){
NoteInfo noteInfo = new NoteInfo();
noteInfo.setId(allNotes.getString(allNotes.getColumnIndex(Note._id)));
noteInfo.setTitle(allNotes.getString(allNotes.getColumnIndex(Note.title)));
noteInfo.setContent(allNotes.getString(allNotes.getColumnIndex(Note.content)));
noteInfo.setDate(allNotes.getString(allNotes.getColumnIndex(Note.time)));
noteList.add(noteInfo);
}
}
public ViewHolder(View itemView) {
if (itemView == null){
throw new IllegalArgumentException("item View can not be null!");
}
this.itemView = itemView;
itemIcon = itemView.findViewById(R.id.rand_icon);
itemNoteTitle = itemView.findViewById(R.id.item_note_title);
itemNoteDate = itemView.findViewById(R.id.item_note_date);
}
<TextView
android:id="@+id/item_note_date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="16sp"
android:gravity="center_vertical"
android:singleLine="true"/>
private void getSelectNoteList(String title){
noteList.clear();
Cursor allNotes = Note.selectNotes(dbHelper,title);
for (allNotes.moveToFirst(); !allNotes.isAfterLast(); allNotes.moveToNext()){
NoteInfo noteInfo = new NoteInfo();
noteInfo.setId(allNotes.getString(allNotes.getColumnIndex(Note._id)));
noteInfo.setTitle(allNotes.getString(allNotes.getColumnIndex(Note.title)));
noteInfo.setContent(allNotes.getString(allNotes.getColumnIndex(Note.content)));
noteInfo.setDate(allNotes.getString(allNotes.getColumnIndex(Note.time)));
noteList.add(noteInfo);
}
}
et_title.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
//当EditText内容发生变化时会调用此方法
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().equals("")){
search_del.setVisibility(View.GONE);
}else{
search_del.setVisibility(View.VISIBLE);
}
String titleChange=et_title.getText().toString();
Log.v("改变数据:",titleChange);
getSelectNoteList(titleChange);
mListAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
这个方法没有第一种方法人性化,用户体验较差
private void setListener(){
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,EditActivity.class);
startActivity(intent);
}
});
selectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String edit_title=et_title.getText().toString();
HashMap<Integer,HashMap<String,Object>> map1=new HashMap<Integer,HashMap<String,Object>>();;
map1=Note.getNote(dbHelper,edit_title);
Log.v("查询",map1.toString());
Intent intent = new Intent(MainActivity.this,SelectActivity.class);
intent.putExtra("edit_title",edit_title );
startActivity(intent);
//getSelectNoteList(edit_title);
}
});
search_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getNoteList();;//初始化数据
et_title.setText("");
mListAdapter.notifyDataSetChanged();//更新RecycleView
}
});
插入图片有多种方式,这里简单介绍两种,分别是从本地获取图片以及拍照直接插入图片。
在AndroidManifest.xml中设置权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
如果是Android7.0以上的版本需要在onCreate()这个方法中加入以下代码,不然启动拍照功能的时候会出现路径不对的问题:
// android 7.0系统解决拍照的问题
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
通过设置intent的类型来启动图片相关的intent
//从相册取图片
public void getPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
startActivityForResult(intent, PHOTO_FROM_GALLERY);
}
通过使用正则表达式来提取图片路径
private static final String regex="content://com.android.providers.media.documents/"
+"document/image%\w{4}";
private static final String reg="file:///storage/emulated/0/\d+.jpg";
获取权限并且启动拍照相关的intent
//拍照取照片
public void takeCamera() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions( this, new String[] { Manifest.permission.CAMERA }, PHOTO_FROM_CAMERA);
}
else {
File file=new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
try {
if(file.exists()){
file.delete();
}
file.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
imageUri=Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent, PHOTO_FROM_CAMERA);
}
}
把图片转换形式并加入文本框,执行保存时把图片存进数据库
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ContentResolver resolver = getContentResolver();
super.onActivityResult(requestCode, resultCode, data);
//第一层switch
switch (requestCode) {
case PHOTO_FROM_GALLERY:
//第二层switch
switch (resultCode) {
case RESULT_OK:
if (data != null) {
Uri uri = data.getData();//获取Intent uri
Bitmap bitmap = null;
//判断该路径是否存在
try {
Bitmap originalBitmap = BitmapFactory.decodeStream(resolver.openInputStream(uri));
bitmap = resizeImage(originalBitmap, 200, 200);//图片缩放
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(bitmap != null){//如果图片存在
//将选择的图片追加到EditText中光标所在位置
int index = mText.getSelectionStart(); //获取光标所在位置
Editable edit_text = mText.getEditableText();//编辑文本框
if(index <0 || index >= edit_text.length()){
edit_text.append(uri.toString());
updateNoteText(mText.getText().toString());//更新到数据库
}else{
edit_text.insert(index,uri.toString());
updateNoteText(mText.getText().toString());//更新到数据库
}
}else{
Toast.makeText(NoteEditor.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
}
break;
case RESULT_CANCELED:
break;
}
break;
case PHOTO_FROM_CAMERA:
if (resultCode == RESULT_OK) {
Bitmap originalBitmap1=null;
//判断图片是否存在
try{
originalBitmap1=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
}catch (FileNotFoundException e){
e.printStackTrace();
}
if(originalBitmap1 != null){//如果图片存在保存URI
//将选择的图片追加到EditText中光标所在位置
int index = mText.getSelectionStart(); //获取光标所在位置
Editable edit_text = mText.getEditableText();//编辑文本框
if(index <0 || index >= edit_text.length()){
edit_text.append(imageUri.toString());
updateNoteText(mText.getText().toString());//更新到数据库
}else{
edit_text.insert(index, imageUri.toString());
updateNoteText(mText.getText().toString());//更新到数据库
}
}else{
Toast.makeText(NoteEditor.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
} else {
Log.e("result", "is not ok" + resultCode);
}
break;
default:
break;
}
}
图片缩放
/**
* 图片缩放
* @param originalBitmap 原始的Bitmap
* @param newWidth 自定义宽度
* @return 缩放后的Bitmap
*/
private Bitmap resizeImage(Bitmap originalBitmap, int newWidth, int newHeight){
int width = originalBitmap.getWidth();
int height = originalBitmap.getHeight();
//定义欲转换成的宽、高
// int newWidth = 200;
// int newHeight = 200;
//计算宽、高缩放率
float scanleWidth = (float)newWidth/width;
float scanleHeight = (float)newHeight/height;
//创建操作图片用的matrix对象 Matrix
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scanleWidth,scanleHeight);
//旋转图片 动作
//matrix.postRotate(45);
// 创建新的图片Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(originalBitmap,0,0,width,height,matrix,true);
return resizedBitmap;
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!