android 应用--->睡眠助手功能实现

      

代码下载见:http://download.csdn.net/source/3477225


上篇文章上纪录了睡眠助手的UI的实现。现在纪录下睡眠助手功能的实现

      上篇文章的时间和开关设置是单独的按钮实现的,后来发现这样设计不合理,就把时间设置改成了只有在选中了开关设置的开之后才可以设置时间。整合代码如下:

//设置开关按钮事件监听
    class SetOnOffListener implements OnClickListener {

    	AlertDialog singleDialog = null ;
		@Override
		public void onClick(View v) {
			
			final String[] strs = new String[]{"开","关"};
			
			singleDialog = new AlertDialog.Builder(HelperMain.this)
							.setTitle("设置开关")
							.setIcon(android.R.drawable.ic_dialog_info)
							.setSingleChoiceItems(strs, 0,
									new DialogInterface.OnClickListener() {
										
										@Override
										public void onClick(DialogInterface dialog, int which) {
											if(strs[which].equals("开")) {
												helper = true ;
												setTime();
												//openHelper();
												
											} else if(strs[which].equals("关")){
												close();
												helper = false ;
												onOffShow.setText("关");
												Toast.makeText(HelperMain.this, "助手已关", Toast.LENGTH_SHORT).show();
											}
											singleDialog.dismiss();
											SharedPreferences uiState = getPreferences(0);
											SharedPreferences.Editor editor = uiState.edit();
											editor.putBoolean("on_off", helper);
											editor.commit();
										}
									})
									.create();
			singleDialog.show();
		}
    	
    }



这里是时间的设置:

public void setTime() {
    	final Time t = new Time();
    	t.setToNow();
    	new TimePickerDialog(HelperMain.this, new TimePickerDialog.OnTimeSetListener() {
			
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				String time ;
				if(hourOfDay > 12) {
					if((hourOfDay - 12) >= 10) {
						time = (hourOfDay - 12) + " : " + minute + "  pm" ;
					} else {
						time = "0" + (hourOfDay - 12) + " : " + minute + "  pm" ;
					}
				} else if(hourOfDay == 12) {
					time = hourOfDay + " : " + minute + "  pm" ;
				}else {
					if(hourOfDay >= 10){
						time = hourOfDay + " : " + minute + "  am" ;
					} else {
						time = "0" + hourOfDay + " : " + minute + "  am" ;
					}
				}
				//calendar.setTimeInMillis(System.currentTimeMillis());
				calendar.set(t.year,t.month,t.monthDay,hourOfDay,minute,0);
				timeShow.setText(time);
				SharedPreferences uiState = getPreferences(0);
				SharedPreferences.Editor editor = uiState.edit();
				editor.putString("time", time);
				//一定要记得在存入数据之后要调用commit方法,不然getXXX的时候会获取不到值
				editor.commit();
				if(helper) {
					openHelper();
					onOffShow.setText("开");
				}
			}
		}, t.hour, t.minute, false).show();
    	
    }



到这里就完成了开关,时间和回复内容的设置。



你可能感兴趣的:(android,UI,String,null,Class,dialog)