編輯:關於Android編程
CountDownTimer:
Schedule a countdown until a time in the future, with regular notifications on intervals along the way.
倒計時這個類比較簡單,可以學習這樣來設計類,下面看這個類的一個簡單應用的例子:
這麼個小例子也需要優化,優化之前什麼也沒考慮只實現了效果, 簡單優化下增加了了多次點擊Dlialog不重復創建和取消countdowntimer任務的代碼;
簡單優化後:
package com.example.dliagdemo; import android.app.Activity; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.CountDownTimer; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button btnSure; private MainActivity instance; private CountDownTimer mDownTimer; private Dialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); instance = this; Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (dialog == null) showDialog(); else if (!dialog.isShowing()) showDialog(); } }); } private void showDialog() { dialog = new Dialog(instance); LayoutInflater inflater = LayoutInflater.from(instance); View v = inflater.inflate(R.layout.itmes, null); btnSure = (Button) v.findViewById(R.id.buttonSure); btnSure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(instance, "游戲開始", Toast.LENGTH_SHORT).show(); if (dialog != null) { dialog.dismiss(); } } }); mDownTimer = new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { if (btnSure != null) { btnSure.setText("自動開始(" + millisUntilFinished / 1000 + "/s)"); } } public void onFinish() { if (btnSure != null) { btnSure.performClick(); } } }.start(); dialog.setContentView(v); dialog.setTitle("開始游戲"); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (mDownTimer != null) { mDownTimer.cancel(); mDownTimer = null; } } }); dialog.show(); } }
package com.example.dliagdemo; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.os.CountDownTimer; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button btnSure; private MainActivity instance; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); instance =this; Button btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(); } }); } private void showDialog() { final Dialog dialog = new Dialog(instance); LayoutInflater inflater = LayoutInflater.from(instance); View v =inflater.inflate(R.layout.itmes, null); btnSure = (Button)v.findViewById(R.id.buttonSure); btnSure.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(instance, "游戲開始", Toast.LENGTH_SHORT).show(); if (dialog!=null&&dialog.isShowing()) { dialog.dismiss(); } } }); new CountDownTimer(10000, 1000) {//總時間, 間隔時間 public void onTick(long millisUntilFinished) { btnSure.setText("即將開始(" + millisUntilFinished / 1000+"/s)"); } public void onFinish() { if (dialog!=null&&dialog.isShowing()) { btnSure.performClick();//點擊按鈕 dialog.dismiss(); } } }.start(); dialog.setContentView(v); dialog.setTitle("開始游戲"); dialog.show(); } }
其實ViewFlipper工作機制很簡單,如上圖,就是將添加到ViewFlipper中的子View按照順序定時的顯示是其中一個子View,其他的子View設置為Gone狀
Paint與Canvas下面先說下Paint的基本設置函數:paint.setAntiAlias(true);//抗鋸齒功能paint.setColor(Color.RE
谷歌官方推出Material Design 設計理念已經有段時間了,為支持更方便的實現Material Design設計效果,官方給出了Android support d
引言Android 中的Drawable是一個抽象的概念,換言之所有能被畫出來的都可以定義成Drawable(A Drawable is a general abstra