現在這個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相關內容:
- android怎麼在radiobutton旁邊添加view
- Android如何擷取MD5 certification key
- Android如何給電子郵件內容添加新的一行
- android中的預設表徵圖在哪個路徑下能找到