Android开发之倒计时应用(二)
2015-02-27 15:40
267 查看
如需转载,请注明出处,谢谢!
从这篇文章开始进入代码细节的实现。关于倒计时应用的介绍可以看上一篇文章。
这篇文章主要是UI的实现,倒计时的实现主要是通过Service来实现的,会在下一篇文章中介绍。
变量说明:
int currentProgress = 0; // 当前过了多少秒,默认为0
int progress = 0; // 需要倒计时的时间,即从输入框取出来的值
ConstantUtil类:主要用来保存一些静态常量。
先从输入框开始:
输入框部分倒不是很难,但有一点要注意的就是要判断输入框是否有输入,否则,启动倒计时的时候可能会报错。
然后是背景图的更换,步骤是这样的,长按背景图弹出上下文菜单,然后再根据需要选择,上个图先。
上下文菜单的创建:
上下文菜单选项的点击事件,需要重写Activity的onMenuItemSelected方法
最后以三个按钮的点击事件作为这篇文章的结束。
开始按钮:
暂停按钮:
停止按钮:
好了,这篇文章就先介绍到这里了,剩下的主要是Service和BroadcastReceiver的实现,放到后面再讲吧。
如果有什么问题或建议,欢迎指出。
从这篇文章开始进入代码细节的实现。关于倒计时应用的介绍可以看上一篇文章。
这篇文章主要是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; }
好了,这篇文章就先介绍到这里了,剩下的主要是Service和BroadcastReceiver的实现,放到后面再讲吧。
如果有什么问题或建议,欢迎指出。
相关文章推荐
- 【Android应用开发】Timer的使用,制作游戏倒计时进度条
- Android开发之倒计时应用(一)
- Android开发之倒计时应用(三)
- Android应用界面开发——Handler(实现倒计时)
- ACR122U Android端应用开发入道指南
- Android应用开发中三种常见的图片压缩方法
- golang开发android应用(一) - go语言android应用环境搭建
- Android应用开发中三种常见的图片压缩方法
- Android应用开发SharedPreferences存储数据的使用方法
- 开发你自己的Android 授权管理器 (AccountManager的使用以及应用场景)
- android应用开发全程实录-你有多熟悉listview?
- Android应用开发必备的20条技能
- Android应用开发-网络编程
- Android WIFI应用简单开发实例
- ANDROID应用开发架构概述
- Android应用开发性能优化完全分析
- 不懂技术,怎样开发Android应用?
- 开发第一个Android应用之前你需要知道的六件事
- Android平板上开发应用的一点心得——精确适配不同的dpi和屏幕尺寸
- android launcher开发(7) 启动应用