如需转载,请注明出处,谢谢!
从这篇文章开始进入代码细节的实现。关于倒计时应用的介绍可以看上一篇文章。
这篇文章主要是UI的实现,倒计时的实现主要是通过Service来实现的,会在下一篇文章中介绍。
变量说明:
int currentProgress = 0; // 当前过了多少秒,默认为0
int progress = 0; // 需要倒计时的时间,即从输入框取出来的值
ConstantUtil类:主要用来保存一些静态常量。
先从输入框开始:
输入框部分倒不是很难,但有一点要注意的就是要判断输入框是否有输入,否则,启动倒计时的时候可能会报错。
String string = editTime.getText().toString();
// 如果edittext中没有输入
if (string == null || string.equals("")) {
toast = Toast.makeText(getApplicationContext(), getResources().getString(R.string.hint), SHORT);
toast.show();
}
上下文菜单的创建:
// 上下文菜单监听器
private class CreateContextMenuListener implements OnCreateContextMenuListener {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, DEFAULT_BG, Menu.NONE, "默认背景");
menu.add(Menu.NONE, CHANGE_BG, Menu.NONE, "更换背景");
menu.add(Menu.NONE, DELETE_BG, Menu.NONE, "删除背景");
}
}
// 设置监听器
relativeLayout.setOnCreateContextMenuListener(new CreateContextMenuListener());
上下文菜单选项的点击事件,需要重写Activity的onMenuItemSelected方法
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
int id = item.getItemId();
// 从图库中选取图片作为背景
if (id == CHANGE_BG) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// 到图库选择一张图片并返回结果
startActivityForResult(intent, REQUEST_IMAGE);
} else if (id == DELETE_BG) {
bgPicturePath = null;
relativeLayout.setBackgroundResource(0);
} else if (id == DEFAULT_BG) {
relativeLayout.setBackgroundResource(R.drawable.bg);
bgPicturePath = null;
}
return super.onMenuItemSelected(featureId, item);
}
从图库返回结果后,需要对结果做出处理,所以我们需要重写Activity的onActivityResult方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE && resultCode == RESULT_OK && data != null) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
// 获得背景图片路径
bgPicturePath = cursor.getString(columnIndex);
cursor.close();
// 将Bitmap对象转换为Drawable对象
BitmapDrawable bd = new BitmapDrawable(getResources(), bgPicturePath);
relativeLayout.setBackground(bd);
}
}
最后以三个按钮的点击事件作为这篇文章的结束。
开始按钮:
private void startTimer() {
String string = editTime.getText().toString();
// 如果edittext中没有输入
if (string == null || string.equals("")) {
toast = Toast.makeText(getApplicationContext(), getResources().getString(R.string.hint), SHORT);
toast.show();
return;
}
progress = Integer.parseInt(string);
showView.setText((progress - currentProgress) + "");
// 启动服务进行倒计时
Intent intent = new Intent(ConstantUtil.START);
intent.setClass(getApplicationContext(), TimerService.class);
intent.putExtra(ConstantUtil.CURRENT_PROGRESS, currentProgress);
intent.putExtra(ConstantUtil.PROGRESS, progress);
startService(intent);
}
private void pauseTimer() {
Intent intent = new Intent(ConstantUtil.PAUSE);
intent.setClass(getApplicationContext(), TimerService.class);
startService(intent);
}
停止按钮:
private void stopTimer() {
Intent intent = new Intent(ConstantUtil.STOP);
intent.setClass(getApplicationContext(), TimerService.class);
startService(intent);
showView.setText(getResources().getString(R.string.default_text));
// 重置currentProgress
currentProgress = 0;
}
如果有什么问题或建议,欢迎指出。