教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 实用模板 >

Android原生模块DeskClock分析1(4)

来源:网络收集 时间:2026-07-05
导读: DeskClock分析文档 用到了newView和bindView,通过前面的介绍,我们知道这两个方法的功能是:当列表排满时,第一次调用newView,之后只调用bindView就可以了。 private class AlarmTimeAdapter extends CursorAdapte

DeskClock分析文档

用到了newView和bindView,通过前面的介绍,我们知道这两个方法的功能是:当列表排满时,第一次调用newView,之后只调用bindView就可以了。 private class AlarmTimeAdapter extends CursorAdapter {

public AlarmTimeAdapter(Context context, Cursor cursor) { super(context, cursor); Log.i(\ }

public View newView(Context context, Cursor cursor, ViewGroup parent) { View ret = mFactory.inflate(R.layout.alarm_time, parent, false);

DigitalClock digitalClock =

(DigitalClock) ret.findViewById(R.id.digitalClock); digitalClock.setLive(false); Log.i(\ return ret; }

public void bindView(View view, Context context, Cursor cursor) { Log.i(\

final Alarm alarm = new Alarm(cursor);

View indicator = view.findViewById(R.id.indicator);

// Set the initial resource for the bar image. final ImageView barOnOff =

(ImageView) indicator.findViewById(R.id.bar_onoff); barOnOff.setImageResource(alarm.enabled ?

R.drawable.ic_indicator_on : R.drawable.ic_indicator_off);

// Set the initial state of the clock \ final CheckBox clockOnOff =

(CheckBox) indicator.findViewById(R.id.clock_onoff); clockOnOff.setChecked(alarm.enabled);

// Clicking outside the \ indicator.setOnClickListener(new OnClickListener() { public void onClick(View v) { clockOnOff.toggle();

updateIndicatorAndAlarm(clockOnOff.isChecked(), barOnOff, alarm); Log.i(\ } });

11

DeskClock分析文档

DigitalClock digitalClock =

(DigitalClock) view.findViewById(R.id.digitalClock);

// set the alarm text

final Calendar c = Calendar.getInstance(); c.set(Calendar.HOUR_OF_DAY, alarm.hour); c.set(Calendar.MINUTE, alarm.minutes); digitalClock.updateTime(c);

digitalClock.setTypeface(Typeface.DEFAULT);

// Set the repeat text or leave it blank if it does not repeat. TextView daysOfWeekView =

(TextView) digitalClock.findViewById(R.id.daysOfWeek); final String daysOfWeekStr =

alarm.daysOfWeek.toString(AlarmClock.this, false); if (daysOfWeekStr != null && daysOfWeekStr.length() != 0) { Log.i(\

daysOfWeekView.setText(daysOfWeekStr); daysOfWeekView.setVisibility(View.VISIBLE); } else {

Log.i(\

daysOfWeekView.setVisibility(View.GONE); }

// Display the label TextView labelView =

(TextView) view.findViewById(R.id.label); if (alarm.label != null && alarm.label.length() != 0) { Log.i(\

labelView.setText(alarm.label); labelView.setVisibility(View.VISIBLE); } else {Log.i(\

labelView.setVisibility(View.GONE); } } };

· 通过onCreateContextMenu方法,长按选中的闹钟,会显示对闹钟操作的选项 @Override

public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) { // Inflate the menu from xml.

getMenuInflater().inflate(R.menu.context_menu, menu);

12

DeskClock分析文档

// Use the current item to create a custom view for the header.

final AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo; final Cursor c =

(Cursor) mAlarmsList.getAdapter().getItem((int) info.position); final Alarm alarm = new Alarm(c);

// Construct the Calendar to compute the time. final Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, alarm.hour); cal.set(Calendar.MINUTE, alarm.minutes); final String time = Alarms.formatTime(this, cal);

// Inflate the custom view and set each TextView's text.

final View v = mFactory.inflate(R.layout.context_menu_header, null); TextView textView = (TextView) v.findViewById(R.id.header_time); textView.setText(time);

textView = (TextView) v.findViewById(R.id.header_label); textView.setText(alarm.label);

// Set the custom view on the menu. menu.setHeaderView(v); Log.i(\

// Change the text based on the state of the alarm. if (alarm.enabled) {Log.i(\

menu.findItem(R.id.enable_alarm).setTitle(R.string.disable_alarm); }

}

· 当点击menu时,通过重写onOptionsItemSelected,弹出菜单选项 @Override

public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) {

case R.id.menu_item_settings:Log.i(\ startActivity(new Intent(this, SettingsActivity.class)); return true;

case R.id.menu_item_desk_clock:Log.i(\ startActivity(new Intent(this, DeskClock.class)); return true;

case R.id.menu_item_add_alarm:Log.i(\ addNewAlarm(); return true;

default:Log.i(\ break;

13

DeskClock分析文档

…… 此处隐藏:2012字,全部文档内容请下载后查看。喜欢就下载吧 ……

Android原生模块DeskClock分析1(4).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/520580.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)