团队冲刺第二阶段05
徐姣美:今天尝试做了做闹钟提醒,学了学相关的内容。
代码:
public class AlarmView extends LinearLayout { @Override protected void onFinishInflate() { super.onFinishInflate(); btnAddAlarm = (Button)findViewById(R.id.btnAddAlarm); lvAlarmList = (ListView)findViewById(R.id.lvAlarmList); lvAlarmList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { new AlertDialog.Builder(getContext()).setTitle("操作选项").setItems(new CharSequence[]{"删除","添加"}, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case 0: deleteAlarm(position); break; case 1: addAlarm(); default: break; } } }).setNegativeButton("取消",null).show(); return true; } }); adapter = new ArrayAdapter<AlarmData>(getContext(),android.R.layout.simple_list_item_1); lvAlarmList.setAdapter(adapter); readSaveAlarmList(); //adapter.add(new AlarmData(System.currentTimeMillis())); alarmManager = (AlarmManager)getContext().getSystemService(Context.ALARM_SERVICE); calendar = Calendar.getInstance(); btnAddAlarm = (Button)findViewById(R.id.btnAddAlarm); btnAddAlarm.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ addAlarm(); } }); } private void deleteAlarm(int position){ AlarmData ad = adapter.getItem(position); adapter.remove(ad); adapter.notifyDataSetChanged(); saveAlarmList(); alarmManager.cancel(PendingIntent.getBroadcast(getContext(),ad.getId(),new Intent(getContext(),AlarmReceiver.class),0)); } private void addAlarm(){ Calendar c = Calendar.getInstance(); c.setTimeInMillis(System.currentTimeMillis()); new TimePickerDialog(getContext(),new TimePickerDialog.OnTimeSetListener(){ @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY,hourOfDay); calendar.set(Calendar.MINUTE,minute); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); Calendar currentTime = Calendar.getInstance(); if(calendar.getTimeInMillis()<=currentTime.getTimeInMillis()){ calendar.setTimeInMillis(calendar.getTimeInMillis()+24*60*60*1000); } AlarmData ad = new AlarmData(calendar.getTimeInMillis()); adapter.add(ad); adapter.notifyDataSetChanged(); alarmManager.set(AlarmManager.RTC_WAKEUP, ad.getTime(),PendingIntent.getBroadcast(getContext(), ad.getId(),new Intent(getContext(),AlarmReceiver.class),0)); saveAlarmList(); } },c.get(Calendar.HOUR_OF_DAY),c.get(Calendar.MINUTE),true).show(); } private void saveAlarmList(){ Editor editor; editor = getContext().getSharedPreferences(AlarmView.class.getName(), Context.MODE_PRIVATE).edit(); StringBuffer sb = new StringBuffer(); for (int i =0; i<adapter.getCount();i++){ sb.append(adapter.getItem(i).getTime()).append(","); } if(sb.length()>1){ String content = sb.toString().substring(0,sb.length()-1); editor.putString(KEY_ALARM_LIST,content); System.out.println(content); }else { editor.putString(KEY_ALARM_LIST,null); } editor.commit(); } private void readSaveAlarmList(){ SharedPreferences sp = getContext().getSharedPreferences( AlarmView.class.getName(),Context.MODE_PRIVATE); String content = sp.getString(KEY_ALARM_LIST,null); if(content != null){ String[] timeStrings = content.split(","); for(String string : timeStrings){ adapter.add(new AlarmData(Long.parseLong(string))); } } } private Button btnAddAlarm; private ListView lvAlarmList; private static final String KEY_ALARM_LIST = "alarmList"; private ArrayAdapter<AlarmData> adapter; private AlarmManager alarmManager; private Calendar calendar; private static class AlarmData{ private String timeLable = ""; private long time = 0; private Calendar date; public AlarmData(long time){ this.time = time; date = Calendar.getInstance(); date.setTimeInMillis(time); timeLable = String.format("%02d月%02d日 %02d:%02d", date.get(Calendar.MONTH)+1, date.get(Calendar.DAY_OF_MONTH), date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE)); } public AlarmData(String ad){ this.timeLable = ad; } public void setTime(long time){ this.time = time; } public long getTime(){ return time; } public void setTimeLable(String timeLable){ this.timeLable = timeLable; } public String getTimeLable(){ return timeLable; } @Override public String toString() { return getTimeLable(); } public int getId(){ return (int)(getTime()/1000/60); } } }
申澳宇:做了主界面中便签的显示,可以显示为扩展试图、缩略试图。
代码:
private void toggleListViewItem (View view, int position) { Note note = listAdapter.getItem(position); LinearLayout cardLayout = view.findViewById(R.id.card_layout); if (!getSelectedNotes().contains(note)) { getSelectedNotes().add(note); listAdapter.addSelectedItem(position); cardLayout.setBackgroundColor(getResources().getColor(R.color.list_bg_selected)); } else { getSelectedNotes().remove(note); listAdapter.removeSelectedItem(position); listAdapter.restoreDrawable(note, cardLayout); } prepareActionModeMenu(); if (getSelectedNotes().isEmpty()) { finishActionMode(); } } private void initListView () { list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); list.setItemsCanFocus(false); list.setOnItemLongClickListener((arg0, view, position, arg3) -> { if (getActionMode() != null) { return false; } mainActivity.startSupportActionMode(new ModeCallback()); toggleListViewItem(view, position); setCabTitle(); return true; }); list.setOnItemClickListener((arg0, view, position, arg3) -> { if (getActionMode() == null) { editNote(listAdapter.getItem(position), view); return; } toggleListViewItem(view, position); setCabTitle(); }); listRoot.setOnViewTouchedListener(this); }
刘贺鑫:完善了搜索功能,能够在主页面搜索笔记,也能查看未完成的待办事项。
代码:
@SuppressLint("NewApi") private void initSearchView (final Menu menu) { if (mainActivity == null) { return; } searchMenuItem = menu.findItem(R.id.menu_search); SearchManager searchManager = (SearchManager) mainActivity.getSystemService(Context.SEARCH_SERVICE); searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search)); searchView.setSearchableInfo(searchManager.getSearchableInfo(mainActivity.getComponentName())); searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH); searchView.setOnQueryTextFocusChangeListener((v, hasFocus) -> setActionItemsVisibility(menu, hasFocus)); MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() { boolean searchPerformed = false; @Override public boolean onMenuItemActionCollapse (MenuItem item) { searchQuery = null; if (searchLayout.getVisibility() == View.VISIBLE) { toggleSearchLabel(false); } mainActivity.getIntent().setAction(Intent.ACTION_MAIN); initNotesList(mainActivity.getIntent()); mainActivity.supportInvalidateOptionsMenu(); return true; } @Override public boolean onMenuItemActionExpand (MenuItem item) { searchView.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextSubmit (String arg0) { return prefs.getBoolean("settings_instant_search", false); } @Override public boolean onQueryTextChange (String pattern) { if (prefs.getBoolean("settings_instant_search", false) && searchLayout != null && searchPerformed && mFragment.isAdded()) { searchTags = null; searchQuery = pattern; NoteLoaderTask.getInstance().execute("getNotesByPattern", pattern); return true; } else { searchPerformed = true; return false; } } }); return true; } }); }