Android Studio 的NotePad制作(日志本)
Posted RikkaTheWorld
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Studio 的NotePad制作(日志本)相关的知识,希望对你有一定的参考价值。
自己写的NotePad,一个星期左右的时间。完成了最基本的功能。
但是 界面还是一如既往的shi(因为百度找的图标都不是那种成套的,想找的找不到,干脆下次自己画!)
NotePad的功能无非是对日志的增删改查,这次还加入了Preference的一些配置的设置。
这是主界面,上方的都是已经建好的日志。
下面的数字和图标对应xml控件的功能为:
①:已经写好的日志的个数
②:系统设置
③:新建日志
④:查找
⑤:锁定界面
先从日志本的数据库讲,基础的数据为日志ID,日志标题,日志内容,日志创建时间。
其次为主题颜色的int值,包括橙色、天蓝、红色等等。
接下来是各种xml布局,基本大同小异,自己想怎么弄就怎么弄。
设置了两个存数据的类,一个是NoteVO为存放ID,Title等关于日志的数据,并写了一个DBAccess里面运用SQL的语法对数据库进行增删改查;还有一个是PrefVO,里面存放了用户的密码以及主题颜色、一些设置保存密码的东西。
为日志本设置了密码,默认密码为空,在设置里面可以进行密码的修改。
设置密码之后,用户可以进行app锁定,下次进来后也要用密码解锁。
主界面为 NotePadMainActivity
除了对图标设置监听事件,比较重要的是 重写 onResum,即刷新界面还有判断此时页面是否锁住,这里写了一个框框加载,代码如下:
protected void onResume()
super.onResume();
showProgressDialog();
//判断用户是否上锁,如果上锁,弹出对话框提示用户输入密码
if(PrefVO.ifLocked)
final EditText keytext = new EditText(NotePadMainActivity.this);
keytext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
AlertDialog.Builder builder = new AlertDialog.Builder(NotePadMainActivity.this);
builder.setTitle("请输入密码");
builder.setIcon(R.drawable.suo);
builder.setView(keytext);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
if(PrefVO.userPasswordValue.equals(keytext.getText().toString()))
PrefVO.appLock(false);
Toast.makeText(NotePadMainActivity.this,"已解除锁定",Toast.LENGTH_LONG).show();
else
Toast.makeText(NotePadMainActivity.this,"密码错误",Toast.LENGTH_LONG).show();
NotePadMainActivity.this.finish();
);
builder.setOnCancelListener(new DialogInterface.OnCancelListener()
@Override
public void onCancel(DialogInterface dialog)
NotePadMainActivity.this.finish();
);
builder.create().show();
//刷新界面
private void flush()
PrefVO.dataFlush();
noteVOList = access.findAllNote();
noteBaseAdapter = new NoteBaseAdapter(NotePadMainActivity.this, R.layout.item, noteVOList);
noteList.setAdapter(noteBaseAdapter);
noteNumText.setText(noteVOList.size() + "");
private class OnItemSelectedListener implements AdapterView.OnItemClickListener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
note = noteVOList.get(position);
Intent intent = new Intent();
intent.setClass(NotePadMainActivity.this, NotePadScanActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("note", note);
intent.putExtra("noteBundle", bundle);
NotePadMainActivity.this.startActivity(intent);
private void showProgressDialog()
pgDialog.setTitle("loading....");
pgDialog.setMessage("少女祈祷中.....");
pgDialog.show();
new progressThread().start();
private Handler handler = new Handler()
public void handleMessage(Message msg)
flush();
pgDialog.cancel();
;
private class progressThread extends Thread
@Override
public void run()
try
Thread.sleep(1000);
handler.sendEmptyMessage(1);
catch (InterruptedException e)
e.printStackTrace();
这里对列表日志设置了一个长按选项,长按后出现删除和短信发送的提示。
代码如下:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
super.onCreateContextMenu(menu,v,menuInfo);
menu.setHeaderIcon(R.drawable.shezhi);
menu.setHeaderTitle("日志选项");
menu.add(0,1,1,"删除");
menu.add(0,2,2,"短信发送");
public boolean onContextItemSelected(MenuItem item)
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int index = info.position;
final NoteVO note = noteVOList.get(index);
switch (item.getItemId())
case 1:
AlertDialog.Builder builder = new AlertDialog.Builder(NotePadMainActivity.this);
builder.setTitle("删除");
builder.setIcon(R.drawable.shanchu);
builder.setMessage("你确定要把日志删除吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
DBAccess access = new DBAccess(NotePadMainActivity.this);
access.deleteNote(note);
dialog.dismiss();
Toast.makeText(NotePadMainActivity.this,"已删除",Toast.LENGTH_LONG).show();
flush();
);
builder.setNegativeButton("取消",null);
builder.create().show();
break;
case 2:
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"));
if(!note.getNoteContent().equals(note.getNoteTitle()))
intent.putExtra("sms_body",note.getNoteContent() + "\\n" + note.getNoteTitle());
else intent.putExtra("sms_body",note.getNoteContent());
NotePadMainActivity.this.startActivity(intent);
break;
return super.onContextItemSelected(item);
此为点击设置之后的界面
设置颜色的代码如下:
themeList = (ListPreference) findPreference("themelist");
themeList.setSummary(PrefVO.themeListValue);
themeList.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
@Override
public boolean onPreferenceChange(Preference preference, Object newValue)
String value = (String)newValue;
themeList.setSummary(value);
PrefVO.setThemeListValue(value);
return true;
);
设置密码的话,因为我已经设置过了,所以有显示为旧密码,如果第一次设置密码,他只有输入密码和确认密码,两个dialog代码如下:
builder_1 = new AlertDialog.Builder(NotePadPreferenceActivity.this);
builder_1.setView(linearLayout_1);
builder_1.setTitle("设置新密码");
builder_1.setIcon(R.drawable.suo);
builder_1.setPositiveButton("确定", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String key = newkeyyext.getText().toString();
String keyagain = newkeyagaintext.getText().toString();
if(key.equals("") || keyagain.equals(""))
Toast.makeText(NotePadPreferenceActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
else if(key.equals(keyagain))
PrefVO.setUserPasswordValue(key);
usersafety.setTitle("修改密码" );
else if(!key.equals(keyagain))
Toast.makeText(NotePadPreferenceActivity.this,"两次密码不一致",Toast.LENGTH_LONG).show();
dialog_1.dismiss();
clearText();
);
builder_1.setNegativeButton("取消", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
dialog_1.dismiss();
clearText();
);
dialog_1 = builder_1.create();
builder_2 = new AlertDialog.Builder(NotePadPreferenceActivity.this);
builder_2.setView(linearLayout_2);
builder_2.setIcon(R.drawable.suo);
builder_2.setPositiveButton("确定", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
String oldkey = oldkeytext.getText().toString();
String modifykey = modifykeytext.getText().toString();
String modifyagainkey = modifykeyagaintext.getText().toString();
if(!oldkey.equals(PrefVO.userPasswordValue))
Toast.makeText(NotePadPreferenceActivity.this,"密码错误",Toast.LENGTH_LONG).show();
else if(modifykey.equals(""))
Toast.makeText(NotePadPreferenceActivity.this,"密码不能为空",Toast.LENGTH_LONG).show();
else if(!modifyagainkey.equals(modifyagainkey))
Toast.makeText(NotePadPreferenceActivity.this,"两次输入密码不正确",Toast.LENGTH_LONG).show();
else if(modifykey.equals(modifyagainkey))
PrefVO.setUserPasswordValue(modifykey);
dialog_2.dismiss();
clearText();
);
builder_2.setNegativeButton("取消", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
dialog_2.dismiss();
clearText();
);
dialog_2 = builder_2.create();
//定义的密码为“”,所以根据密码来判断是第一次设置密码还是修改密码
usersafety = findPreference("usersafety");
if(PrefVO.userPasswordValue.equals(""))
usersafety.setTitle("设置新密码");
else
usersafety.setTitle("修改密码");
usersafety.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener()
@Override
public boolean onPreferenceClick(Preference preference)
if(PrefVO.userPasswordValue.equals(""))
dialog_1.show();
else
dialog_2.show();
return false;
);
//此方法清空editview,同时要将光标聚焦到第一个EditView
private void clearText()
newkeyyext.setText("");
newkeyagaintext.setText("");
newkeyyext.requestFocus();
oldkeytext.setText("");
modifykeytext.setText("");
modifykeyagaintext.setText("");
oldkeytext.requestFocus();
接下来为新建日志本,这里自己写了两个分别继承TextView和EditView的子控件:NoteTextView,NoteEditView,做了少许的更改。
进入新建页面后,会将焦点放在content上,然后标题跟随打印内容,直到用户自己想写一个标题的时候。在按下返回键后,重写onBackPressed,将新写的日志内容存入到数据库,页面来到查看页面,Toast打印已保存。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-O8cF2dQW-1587976591599)(//img-blog.csdn.net/20180314120118935?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L3Jpa2thdGhld29ybGQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)]
实现代码如下:
protected void onCreate(Bundle saveInstanceState)
super.onCreate(saveInstanceState);
setContentView(R.layout.edit);
editLayout = findViewById(R.id.editlayout);
editLayout.setBackgroundColor(PrefVO.themeColorValue);
noteTitleText = findViewById(R.id.titleedit);
noteContentText = findViewById(R.id.contentedit);
noteContentText.requestFocus();
noteContentText.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
if(flagTextChanged && (noteTitleText.getText().toString().trim().equals(null) ||
noteTitleText.getText().toString().trim().equals("")))
flagTextChanged = true;
else if(!noteTitleText.getText().toString().equals(noteContentText.getText().toString()))
flagTextChanged = false;
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
if(flagTextChanged)
noteTitleText.setText(noteContentText.getText());
@Override
public void afterTextChanged(Editable s)
);
public void onBackPressed()
super.onBackPressed();
String noteTitle = noteTitleText.getText().toString();
String noteContent = noteContentText.getText().toString();
if (noteTitle.toString().trim().equals("") && noteContent.toString().trim().equals(""))
NotePadNewActivity.this.finish();
else
NoteVO note = new NoteVO();
note.setNoteTitle(noteTitle);
note.setNoteContent(noteContent);
note.setNoteDate(new Date());
DBAccess access = new DBAccess(this);
access.insertNote(note);
Toast.makeText(this,"已保存",Toast.LENGTH_LONG).show();
Intent intent = new Intent();
intent.setClass(this,NotePadScanActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("note",note);
intent.putExtra("noteBundle",bundle);
this.startActivity(intent);
this.finish();
进入查看界面有三个可选操作:编辑,删除,短信发送。编辑即返回到edit的页面。
实现代码如下:
scanLayout = findViewById(R.id.scanlayout);
scanLayout.setBackgroundColor(PrefVO.themeColorValue);
noteTitleText = findViewById(R.id.titlescan);
noteContentText = findViewById(R.id.contentscan);
noteContentText.setMovementMethod(ScrollingMovementMethod.getInstance());
notedateText = findViewById(R.id.datescan);
intent = this.getIntent();
Bundle bundle = intent.getBundleExtra("noteBundle");
noteVO = bundle.getParcelable("note");
noteTitleText.setText(noteVO.getNoteTitle());
notedateText.setText(ConvertDate.datetoString(noteVO.getNoteDate()));
noteContentText.setText(noteVO.getNoteContent());
bianji = findViewById(R.id.bianji);
bianji.setOnTouchListener(new View.OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
Intent intent = new Intent();
intent.setClass(NotePadScanActivity.this,NotePadEditActivity.class);
Bundle bundle= new Bundle();
bundle.putParcelable("note",noteVO);
intent.putExtra("noteBundle",bundle);
NotePadScanActivity.this.startActivity(intent);
NotePadScanActivity.this.finish();
return false;
);
duanxin = findViewById(R.id.duanxin);
duanxin.setOnTouchListener(new View.OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"));
if(!noteVO.getNoteContent().equals(noteVO.getNoteTitle()))
intent.putExtra("sms_body", noteVO.getNoteTitle() + "\\n" + noteVO.getNoteContent());
else
intent.putExtra("sms_body", noteVO.getNoteContent());
NotePadScanActivity.this.startActivity(intent);
return false;
);
shanchu = findViewById(R.id.shanchu);
shanchu.setOnTouchListener(new View.OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
AlertDialog.Builder builder = new AlertDialog.Builder(NotePadScanActivity.this);
builder.setTitle("删除");
builder.setIcon(R.drawable.shanchu);
builder.setMessage("你确定要把日志删除吗?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
@Override
public void onClick(DialogInterface dialog, int which)
DBAccess access = new DBAccess(NotePadScanActivity.this);
access.deleteNote(noteVO);
dialog.dismiss();
Toast.makeText(NotePadScanActivity.this, "已删除", Toast.LENGTH_LONG).show();
NotePadScanActivity.this.finish();
);
builder.setNegativeButton("取消", null);
builder.create().show();
return false;
);
最后则是查找操作
在mainfest设置为对话框形式,写了一个NoteCursorAdapter继承CursorAdapter,对列表日志的标题和内容进行查找,用到AutoCompleteTextView代码如下:
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
this.setContentView(R.layout.search);
access = new DBAccess(this);
c = access.selectAllNoteCursor(null,null);
noteCursorAdapter = new NoteCursorAdapter(this,c,true);
searchTextView = findViewById(R.id.searchtext);
searchTextView.setDropDownHeight(0);
searchTextView.requestFocus();
searchTextView.setAdapter(noteCursorAdapter);
searchListView = findViewById(R.id.searchList);
searchListView.setAdapter(noteCursorAdapter);
searchListView.setOnItemClickListener(new OnItemSelectListener());
private class OnItemSelectListener implements AdapterView.OnItemClickListener
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
note = noteCursorAdapter.getList().get(position);
Intent intent = new Intent();
intent.setClass(NotePadSearchActivity.this,NotePadScanActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("note",note);
intent.putExtra("noteBundle",bundle);
NotePadSearchActivity.this.startActivity(intent);
至此为止,功能基本实现,其他的代码就不放了,等下挂GIT
emmm由于在debug的时候有很多数据库的单词填错或者没写空格,几乎浪费了一个上午的时间,还有参考的书籍实在太过老旧,menuItem在AS3.0已经不用,在那上面还是浪费了很多时间。
这次的话我注意了代码的美观,尽量让代码看起来易懂一点,虽然注释也少了一点…
以上是关于Android Studio 的NotePad制作(日志本)的主要内容,如果未能解决你的问题,请参考以下文章
如何在Android Studio的活动底部制作Android xml Linearlayout?