NotePad功能扩展(2)

break;

case NotePad.Notes.RED_COLOR:

view.setBackgroundColor(Color.rgb(244, 149, 133));

break;

default:

view.setBackgroundColor(Color.rgb(255, 255, 255));

break;

}

}

}

  1. 将NoteList和NoteSearch里的适配器改为MyCursorAdapter

MyCursorAdapter adapter

= new MyCursorAdapter(

this,

R.layout.noteslist_item,

cursor,

dataColumns,

viewIDs

);

  1. 新建colors.xml和color_select.xml。选中时的笔记时笔记的背景颜色会改变

#be96df

<item android:drawable=“@color/color1”

android:state_pressed=“true”/>

  1. 在notelist_item.xml里为控件添加选择器

android:background=“@drawable/color_select”

  1. 模拟器截图

UI

Ui

ui

select

  • 更换背景颜色
  1. 为了编辑笔记的时候可以显示背景颜色(显示时能查询到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

};

  1. 在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;

}

}

  1. 在editor_options_menu.xml中添加一个更改背景的按钮

<item android:id=“@+id/menu_color”

android:title=“color”

android:icon=“@drawable/ic_menu_edit”

android:showAsAction=“always”/>

  1. 往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;

  1. 新建选择背景颜色的布局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”/>

  1. 新建选择颜色的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”/>

  1. 模拟器截图

change1

change2

change3

change4

作者 穆昕雨
原文链接

最后

考虑到文章的篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档

喜欢的朋友可以关注、转发、点赞 感谢!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!
_70)

change4

作者 穆昕雨
原文链接

最后

考虑到文章的篇幅问题,我把这些问题和答案以及我多年面试所遇到的问题和一些面试资料做成了PDF文档

[外链图片转存中…(img-d7uZy4W1-1714938381082)]

[外链图片转存中…(img-yjTmgIQM-1714938381084)]

喜欢的朋友可以关注、转发、点赞 感谢!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》点击传送门,即可获取!

  • 23
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值