編輯:關於Android編程
ProgressDialog類似於ProgressBar類。
用於顯示一個過程,通常用於耗時操作。
幾個方法介紹:
1.setMax()
設置對話框中進度條的最大值。
2.setTile()
設置標題。
3.setProgressStyl()
設置對話框中進度條的樣式。例如:環形和水平。
參數:
ProgressDialog.STYLE_SPINNER 環形精度條
ProgressDialog.STYLE_HORIZONTAL 水平樣式的進度條
4.setMessage()
設置顯示的內容。
演示實例:
用於模擬一個耗時的連接操作。
圖:
MainActivity.java
public class MainActivity extends Activity { Button button, button2; ProgressDialog progressDialog; public final static String ABOUT_SERVER = "Connect"; // 信息廣播接收 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context c, Intent intent) { String str = intent.getStringExtra("DATA"); if (str.equals("0")) { progressDialog.setMessage("開始連接"); } else if (str.equals("1")) { progressDialog.setMessage("連接完畢"); } else { progressDialog.dismiss(); } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 注冊廣播監聽 IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(ABOUT_SERVER); registerReceiver(broadcastReceiver, intentFilter); } public void click(View v) { progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setMessage("准備建立連接·············"); progressDialog.show(); Intent intent = new Intent(MainActivity.this, ServerActivity.class); startService(intent); } }activity_main.xml
ServerActivity.java
public class ServerActivity extends Service { Handler handler = new Handler() { public void handleMessage(Message msg) { Intent intent = new Intent(MainActivity.ABOUT_SERVER); if (msg.what == 0) { intent.putExtra("DATA", "0"); } else if (msg.what == 1) { intent.putExtra("DATA", "1"); } else { intent.putExtra("DATA", "2"); ServerActivity.this.stopSelf(); } sendBroadcast(intent); }; }; @Override public void onCreate() { super.onCreate(); Thread thread = new Thread(new Runnable() { @Override public void run() { // 要做的事 try { for (int i = 0; i < 3; i++) { Thread.sleep(1000); Message message = new Message(); message.what = i; handler.sendMessage(message); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start(); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } }AndroidManifest.xml
final Message message = new Message(); Thread thread = new Thread(new Runnable() { @Override public void run() { // 要做的事 try { for (int i = 0; i < 3; i++) { Thread.sleep(1000); message.what = i; handler.sendMessage(message); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); thread.start();使用了同一個Message。用的API 19(4.4)與4.4模擬器,不顯示錯誤,只是卡住,用的API 19(4.4)與2.2模擬器就報錯了。
首先看下效果圖一:布局代碼鍵盤由0~9的數字,刪除鍵和完成鍵組成,也可以根據需求通過GridView適配器的getItemViewType方法來定義。點擊鍵的時候背景有變
Service 是Android四大組件之一,通常是在後台運行的,執行一些耗時的操作。對於Service 我們需要掌握的知識點有:1、Service 的生命周期2、Ser
Playback control of audio/video files and streams is managed as a state machine. The
1. 一個簡單的Android 輸入法(1) 使用Android Studio建立一個沒有Activity的項目,對於一個簡單的輸入法而言,MainActivity並不是