break;
case NotePad.Notes.RED_COLOR:
view.setBackgroundColor(Color.rgb(244, 149, 133));
break;
default:
view.setBackgroundColor(Color.rgb(255, 255, 255));
break;
}
}
}
- 将NoteList和NoteSearch里的适配器改为MyCursorAdapter
MyCursorAdapter adapter
= new MyCursorAdapter(
this,
R.layout.noteslist_item,
cursor,
dataColumns,
viewIDs
);
- 新建colors.xml和color_select.xml。选中时的笔记时笔记的背景颜色会改变
#be96df
<item android:drawable=“@color/color1”
android:state_pressed=“true”/>
- 在notelist_item.xml里为控件添加选择器
android:background=“@drawable/color_select”
- 模拟器截图
- 更换背景颜色
- 为了编辑笔记的时候可以显示背景颜色(显示时能查询到color数据),在NoteEditor.java中增加color属性
private static final String[] PROJECTION =
new String[] {
NotePad.Notes._ID,
NotePad.Notes.COLUMN_NAME_TITLE,
NotePad.Notes.COLUMN_NAME_NOTE,
//增加 背景颜色
NotePad.Notes.COLUMN_NAME_BACK_COLOR
};
- 在onResume()中增加设置背景颜色的代码
//读取颜色数据
if(mCursor!=null){
mCursor.moveToFirst();
int x = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_BACK_COLOR);
int y = mCursor.getInt(x);
Log.i(“NoteEditor”, “color”+y);
switch (y){
case NotePad.Notes.DEFAULT_COLOR:
mText.setBackgroundColor(Color.rgb(255, 255, 255));
break;
case NotePad.Notes.YELLOW_COLOR:
mText.setBackgroundColor(Color.rgb(247, 216, 133));
break;
case NotePad.Notes.BLUE_COLOR:
mText.setBackgroundColor(Color.rgb(165, 202, 237));
break;
case NotePad.Notes.GREEN_COLOR:
mText.setBackgroundColor(Color.rgb(161, 214, 174));
break;
case NotePad.Notes.RED_COLOR:
mText.setBackgroundColor(Color.rgb(244, 149, 133));
break;
default:
mText.setBackgroundColor(Color.rgb(255, 255, 255));
break;
}
}
- 在editor_options_menu.xml中添加一个更改背景的按钮
<item android:id=“@+id/menu_color”
android:title=“color”
android:icon=“@drawable/ic_menu_edit”
android:showAsAction=“always”/>
- 往NoteEditor.java中添加增加上一步新增的选项的执行语句,在onOptionsItemSelected()的switch中添加代码
case R.id.menu_color://跳转改变颜色的activity
Intent intent = new Intent(null,mUri);
intent.setClass(NoteEditor.this,NoteColor.class);
NoteEditor.this.startActivity(intent);
break;
- 新建选择背景颜色的布局note_color.xml
<ImageButton
android:id=“@+id/color_white”
android:layout_width=“0dp”
android:layout_height=“50dp”
android:layout_weight=“1”
android:background=“#ffffff”
android:onClick=“white”/>
<ImageButton
android:id=“@+id/color_yellow”
android:layout_width=“0dp”
android:layout_height=“50dp”
android:layout_weight=“1”
android:background=“#fdef90”
android:onClick=“yellow”/>
<ImageButton
android:id=“@+id/color_blue”
android:layout_width=“0dp”
android:layout_height=“50dp”
android:layout_weight=“1”
android:background=“#6dc1ed”
android:onClick=“blue”/>
<ImageButton
android:id=“@+id/color_green”
android:layout_width=“0dp”
android:layout_height=“50dp”
android:layout_weight=“1”
android:background=“#94e49f”
android:onClick=“green”/>
<ImageButton
android:id=“@+id/color_red”
android:layout_width=“0dp”
android:layout_height=“50dp”
android:layout_weight=“1”
android:background=“#f19696”
android:onClick=“red”/>
- 新建选择颜色的NoteColor.java并注册
public class NoteColor extends Activity {
private Cursor mCursor;
private Uri mUri;
private int color;
private static final int COLUMN_INDEX_TITLE = 1;
private static final String[] PROJECTION = new String[] {
NotePad.Notes._ID,
NotePad.Notes.COLUMN_NAME_BACK_COLOR,
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.note_color);
mUri = getIntent().getData();
mCursor = managedQuery(
mUri,
PROJECTION,
null,
null,
null
);
}
@Override
protected void onResume(){
if(mCursor.moveToFirst()){
color = mCursor.getInt(mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_BACK_COLOR));
Log.i(“NoteColor”, “before”+color);
}
super.onResume();
}
@Override
protected void onPause() {
//将修改的颜色存入数据库
super.onPause();
ContentValues values = new ContentValues();
Log.i(“NoteColor”, “cun”+color);
values.put(NotePad.Notes.COLUMN_NAME_BACK_COLOR, color);
getContentResolver().update(mUri, values, null, null);
int x = mCursor.getColumnIndex(NotePad.Notes.COLUMN_NAME_BACK_COLOR);
int y = mCursor.getInt(x);
Log.i(“NoteColor”, “du”+y);
}
public void white(View view){
color = NotePad.Notes.DEFAULT_COLOR;
finish();
}
public void yellow(View view){
color = NotePad.Notes.YELLOW_COLOR;
finish();
}
public void blue(View view){
color = NotePad.Notes.BLUE_COLOR;
finish();
}
public void green(View view){
color = NotePad.Notes.GREEN_COLOR;
finish();
}
public void red(View view){
color = NotePad.Notes.RED_COLOR;
finish();
}
}
<activity android:name=“NoteColor”
android:theme=“@android:style/Theme.Holo.Light.Dialog”
android:label=“Select Color”/>
- 模拟器截图
作者 穆昕雨
原文链接
最后
考虑到文章的篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档
喜欢的朋友可以关注、转发、点赞 感谢!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
_70)
作者 穆昕雨
原文链接
最后
考虑到文章的篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档
[外链图片转存中…(img-d7uZy4W1-1714938381082)]
[外链图片转存中…(img-yjTmgIQM-1714938381084)]
喜欢的朋友可以关注、转发、点赞 感谢!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!