Android学习之——APP番茄工作法——小结(1)
public class fragment3 extends PreferenceFragment{ public static fragment3 newInstance(Bundle bundle) { fragment3 frag = new fragment3(); frag.setArguments(bundle); return frag; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); Log.v("huahua", "fragment3-->onCreate()"); } }
SharedPreferences mySharedPreferences = getActivity().getSharedPreferences("TomatoCount", Activity.MODE_PRIVATE);//获取SharedPreferences 中的值,TomatoCount表示保存的文件名称 String dateStr = mySharedPreferences.getString("date", "2001-01-01");//获取字符串 todayTomatoCount = mySharedPreferences.getInt("todayTomatoCount", 0);//获取存储的今日番茄时间,获取int型数据,如果不存在默认设置为0 allTomatoCount = mySharedPreferences.getInt("allTomatoCount", 0);//获取存储的合计番茄时间 String dateNowString = (new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())).format(new java.util.Date()); if (!dateStr.equals(dateNowString)) {//判断存储时间是否和当前时间在同一天 todayTomatoCount=0; SharedPreferences.Editor editor = mySharedPreferences.edit(); editor.putInt("todayTomatoCount", todayTomatoCount);//写入数据到Editor 其中第一个参数是字段的名称,第二个参数是字段的值,该写入的是int类型 editor.commit();//提交,写入到xml文件中 }
//修改字体 Typeface fontFace = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Thin.ttf"); tomatoTxtView.setTypeface(fontFace);
public class SettingPreferenceFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener { ListPreference lstPre_TomatoTime_value, lstPre_BreakTime_value; public SettingPreferenceFragment() { // TODO 自动生成的构造函数存根 } @Override public void onCreate(Bundle paramBundle) { // TODO 自动生成的方法存根 super.onCreate(paramBundle); addPreferencesFromResource(R.xml.preferences); SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getActivity()); prefs.registerOnSharedPreferenceChangeListener(this); lstPre_TomatoTime_value=(ListPreference)findPreference("TomatoTime_value"); lstPre_BreakTime_value=(ListPreference)findPreference("BreakTime_value"); lstPre_TomatoTime_value.setSummary(lstPre_TomatoTime_value.getEntry()); lstPre_BreakTime_value.setSummary(lstPre_BreakTime_value.getEntry()); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // TODO 自动生成的方法存根 if (key.equals("TomatoTime_value")) { lstPre_TomatoTime_value.setSummary(lstPre_TomatoTime_value.getEntry()); } if (key.equals("BreakTime_value")) { lstPre_BreakTime_value.setSummary(lstPre_BreakTime_value.getEntry()); } } @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { // TODO 自动生成的方法存根 if (preference.getKey().equals("clearCount")) { alertDialogShow(); } if (preference.getKey().equals("aboutTomatoTask")) { Uri uri = Uri.parse("http://baike.baidu.com/link?url=b7rlhS6YssFup2xqAjnw9__6VsQnyhtVT8Gx_-qwckUE4IZ-ns6i_jw9w_aKH-C_sjWheb9NFR_GZcfUII0bV_"); startActivity(new Intent(Intent.ACTION_VIEW,uri)); } return false; } /** * 显示AlertDialog */ private void alertDialogShow() { new AlertDialog.Builder(getActivity()).setTitle("清除?").setMessage("是否清除计数?\n注:该操作不可逆!").setPositiveButton("清除", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO 自动生成的方法存根 SharedPreferences mySharedPreferences = getActivity().getSharedPreferences("TomatoCount", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = mySharedPreferences.edit(); editor.putInt("todayTomatoCount", 0); editor.putInt("allTomatoCount", 0); editor.commit(); Toast.makeText(getActivity(), "清除成功!", Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO 自动生成的方法存根 } }).create().show(); } }
private TimeCount time;2. new 一个TimeCount, timeSpan是需要倒计时的时间(毫秒),1000是倒计时间隔,这里是一秒
time = new TimeCount(timeSpan, 1000);// 构造CountDownTimer对象 time.start();
class TimeCount extends CountDownTimer { public TimeCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔 } /** * 计时过程显示 */ @Override public void onTick(long millisUntilFinished) { // TODO 自动生成的方法存根 } /** * 计时完毕时触发 */ @Override public void onFinish() { // TODO 自动生成的方法存根 } }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (flag == 2) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { AlertDialog.Builder alertBuilder = new AlertDialog.Builder( MainActivity.this); alertBuilder .setTitle("放弃?") .setMessage("是否放弃这个番茄并退出吗?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO 自动生成的方法存根 time.cancel(); MainActivity.this.finish(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO 自动生成的方法存根 dialog.cancel(); } }).create(); alertBuilder.show(); } } else { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退到主界面", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { time.cancel(); finish(); System.exit(0); } return true; } return super.onKeyDown(keyCode, event); } return true; }
问题七:震动的实现
private Vibrator vibrator;
//开启震动 vibrator =(Vibrator)getSystemService(Context.VIBRATOR_SERVICE); long [] pattern = {200,500,200,500,1200,500,200,500}; // 停止 开启 停止 开启 vibrator.vibrate(pattern,-1); //重复两次上面的pattern 如果只想震动一次,index设为-1
package com.android.tomatotask; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; public class CircleProgressBar extends View { private int maxProgress = 10;//最大进度 private int progress = 0;//当前进度 private int progressStrokeWidth = 6;//线宽 // 画圆所在的矩形区域 RectF oval; Paint paint; public CircleProgressBar(Context context) { super(context); // TODO 自动生成的构造函数存根 } public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO 自动生成的构造函数存根 oval = new RectF(); paint = new Paint(); } public CircleProgressBar(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO 自动生成的构造函数存根 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** * 画最外层的大圆环 */ int centre = getWidth()/2; //获取圆心的x坐标 int radius = (int) (centre - progressStrokeWidth/2); //圆环的半径 paint.setColor(Color.WHITE);//(roundColor); //设置圆环的颜色 paint.setStyle(Paint.Style.STROKE); //设置空心 paint.setStrokeWidth(progressStrokeWidth); //设置圆环的宽度 paint.setAntiAlias(true); //消除锯齿 canvas.drawCircle(centre, centre, radius, paint); //画出圆环 /** * 画圆弧 ,画圆环的进度 */ //设置进度是实心还是空心 paint.setStrokeWidth(progressStrokeWidth); //设置圆环的宽度 paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); //设置进度的颜色 RectF oval = new RectF(centre - radius, centre - radius, centre + radius, centre + radius); //用于定义的圆弧的形状和大小的界限 paint.setStyle(Paint.Style.STROKE); canvas.drawArc(oval, -90, 360 * progress / maxProgress, false, paint); //根据进度画圆弧 绘制白色圆圈,即进度条背景 } public int getMaxProgress(){ return maxProgress; } public void setMaxProgress(int maxProgress){ this.maxProgress = maxProgress; } public void setProgress(int progress){ this.progress = progress; this.invalidate(); } public void setProgressNotInUiThread(int progress){ this.progress = progress; this.postInvalidate(); } }
<com.android.tomatotask.CircleProgressBar android:id="@+id/circleProgressbar" android:layout_width="300dp" android:layout_height="300dp" android:layout_centerInParent="true" /> <android.support.v4.view.ViewPager android:id="@+id/viewpage" android:layout_width="match_parent" android:layout_height="match_parent" />
protected Animation animation;
// 动画资源文件 ID = new int[] { R.anim.my_alpha_action, R.anim.my_scale_action, R.anim.my_rotate_action, R.anim.alpha_scale, R.anim.alpha_rotate, R.anim.scale_rotate, R.anim.alpha_scale_rotate, R.anim.myown_design };
animation = AnimationUtils.loadAnimation(MainActivity.this, ID[randow]);//randow为随机取到0~7的数的随机数 textView.startAnimation(animation);
问题十:针对只有几个确定的数,使用SeekBar
private class SeekBarListener implements SeekBar.OnSeekBarChangeListener { private TextView textView; private int TickStep; private int StartTick; public SeekBarListener(TextView tv, int startTick, int tickStep) { textView = tv; TickStep = tickStep; StartTick = startTick; } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub if (fromUser) { // .. } // 时间=process*步长+初始值 // int progress=seekBar.getProgress(); int curTick = progress + StartTick; int remainder = curTick % TickStep; int halfStep = TickStep % 2 == 0 ? TickStep - TickStep % 2 : TickStep - TickStep % 2 + 1; if (remainder < halfStep) { curTick -= remainder; } else { curTick += (TickStep - remainder); } // seekBar.setProgress(curTick - StartTick); textView.setText(curTick + "min"); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub // 时间=process*步长+初始值 int progress = seekBar.getProgress(); int curTick = progress + StartTick; int remainder = curTick % TickStep; int halfStep = TickStep % 2 == 0 ? TickStep - TickStep % 2 : TickStep - TickStep % 2 + 1; if (remainder < halfStep) { curTick -= remainder; } else { curTick += (TickStep - remainder); } seekBar.setProgress(curTick - StartTick); textView.setText(curTick + "min"); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。