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”/>
- 模拟器截图
作者 穆昕雨
原文链接
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
最后
这里我希望可以帮助到大家提升进阶。
内容包含:Android学习PDF+架构视频+面试文档+源码笔记,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 这几块的内容。非常适合近期有面试和想在技术道路上继续精进的朋友。
喜欢本文的话,不妨给我点个小赞、评论区留言或者转发支持一下呗~
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
id)**
最后
这里我希望可以帮助到大家提升进阶。
内容包含:Android学习PDF+架构视频+面试文档+源码笔记,高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料 这几块的内容。非常适合近期有面试和想在技术道路上继续精进的朋友。
喜欢本文的话,不妨给我点个小赞、评论区留言或者转发支持一下呗~
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!