此为点击设置之后的界面
设置颜色的代码如下:
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 {
ent().equals(noteVO.getNoteTitle())){
intent.putExtra(“sms_body”, noteVO.getNoteTitle() + “\n” + noteVO.getNoteContent());
}
else {
[外链图片转存中…(img-dDuusikh-1646471652058)]
[外链图片转存中…(img-pMYEUvpO-1646471652058)]
领取获取往期Android高级架构资料、源码、笔记、视频。高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter全方面的Android进阶实践技术,群内还有技术大牛一起讨论交流解决问题。