Android实现自定义时钟控件
现在这个DEMO把它封装成自定义控件,对外仅提供了3个公用接口:
1 public void switchClockState(_ClockState state) // 切换时钟状态
2
3 public void saveTimeToSystem() // 保存表盘时间到系统时间
4
5 public void reviseTime() // 恢复当前时间为系统时间
那么在使用起来就相对方便了 看看主Activity里的代码,是不是很简单:
01 public class CustomClockDemoActivity extends Activity {
02 /** Called when the activity is first created. */
03 private CustomClock mCustomClock1;
04 private CustomClock mCustomClock2;
05 private CustomClock mCustomClock3;
06 private Button mBtnSetting;
07 private Button mBtnSave;
08 private Button mBtnNoSave;
09
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setContentView(R.layout.main);
13 init();
14 }
15
16 public void init(){
17 mCustomClock1 = (CustomClock) findViewById(R.id.clock1);
18 mCustomClock1.switchClockState(CustomClock._ClockState.eQ_CLOCK_RUN);
19 mCustomClock2 = (CustomClock) findViewById(R.id.clock2);
20 mCustomClock2.switchClockState(CustomClock._ClockState.eQ_CLOCK_RUN);
21 mCustomClock3 = (CustomClock) findViewById(R.id.clock3);
22 mCustomClock3.switchClockState(CustomClock._ClockState.eQ_CLOCK_RUN);
23 mBtnSetting = (Button) findViewById(R.id.buttonSetting);
24 mBtnSetting.setOnClickListener(new OnClickListener() {
25
26 @Override
27 public void onClick(View v) {
28 // TODO Auto-generated method stub
29 Setting();
30 }
31 });
32
33 mBtnSave = (Button) findViewById(R.id.buttonSave);
34 mBtnSave.setOnClickListener(new OnClickListener() {
35
36 @Override
37 public void onClick(View v) {
38 // TODO Auto-generated method stub
39 Save();
40 }
41 });
42 mBtnNoSave = (Button) findViewById(R.id.buttonNoSave);
43 mBtnNoSave.setOnClickListener(new OnClickListener() {
44
45 @Override
46 public void onClick(View v) {
47 // TODO Auto-generated method stub
48 noSave();
49 }
50 });
51 mCustomClock1.setVisibility(View.INVISIBLE);
52 mCustomClock2.setVisibility(View.GONE);
53 mCustomClock3.setVisibility(View.GONE);
54
55 }
56
57 public void Setting(){
58
59 mCustomClock1.switchClockState(CustomClock._ClockState.eQ_CLOCK_SETTING);
60 }
61
62 public void Save(){
63
64 mCustomClock1.saveTimeToSystem();
65 mCustomClock1.switchClockState(CustomClock._ClockState.eQ_CLOCK_RUN);
66 }
67
68 public void noSave(){
69 mCustomClock1.reviseTime();
70 mCustomClock1.switchClockState(CustomClock._ClockState.eQ_CLOCK_RUN);
71 }
72
73 }
Android相关内容: