Android Studio 的NotePad制作(日志本),详解Android架构进阶面试题

}

}

});

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.ne

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

t/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());

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值