編輯:關於 Android 程式設計
先看 main.xml,這裡放三顆按鈕,分別示範兩種進度對話框與自定義對話框。
自定義對話框的版面 custom_dialog.xml:
自定義對話框的封裝類:
package com.example.progressdialog; import android.app.Dialog; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class CustomDialog { private Context context; Dialog dialog; public CustomDialog(Context context){ this.context = context; dialog = new Dialog(context); } public void show(){ View view = LayoutInflater.from(context).inflate(R.layout.custom_dialog, null); dialog.setContentView(view); dialog.setTitle("自定義的對話框"); ImageView imageView = (ImageView) view.findViewById(R.id.imageView1); imageView.setImageResource(R.drawable.ic_launcher); TextView textView = (TextView) view.findViewById(R.id.textView1); textView.setText("這是自定義對話框"); dialog.show(); } }
主程式。這裡有個實用差異:用 ProgressDialog.show(...) 這種靜態寫法,點擊返回鍵不會有反應;而用 new ProgressDialog 再 show() 的寫法,點返回鍵則能關閉進度對話框。
package com.example.progressdialog; import android.app.Activity; import android.app.ProgressDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button1; private Button button2; private Button button3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { ProgressDialog dialog = new ProgressDialog(MainActivity.this); dialog.setTitle("提示"); dialog.setMessage("正在加載,請稍後..."); dialog.show();// 這樣寫點擊返回鍵可退出 } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { ProgressDialog dialog = new ProgressDialog(MainActivity.this); dialog.setTitle("下載提示"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(50);// 要顯示進度,需加線程 dialog.show(); } }); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { CustomDialog dialog = new CustomDialog(MainActivity.this); dialog.show(); } }); } }
運行結果如下:




用原生 GridView 做一個九宮格功能頁
需求背景 新專案有個頁面要做成九宮格:每個 Item 上方一張圖片、下方一行文字說明,點擊後跳到對應的功能頁。與其去找第三方庫,不如直接用 Android 原生的 Gri
Android 多線程:IntentService 完全詳解
上一篇聊了 HandlerThread,本篇就來看看它在 IntentService 中的實際應用。閱讀前建議先了解 HandlerThread,有助於更好地掌握 Int
Android 自訂狀態欄顏色的實作教學
每次使用 iOS 上的應用時,總會發現狀態欄的顏色能和標題欄保持一致,整體視覺相當協調,使用體驗也很出色。像我一樣好奇心強的人,自然會想:這樣的效果在 Android 上
利用 android-wheel 控件實現省、市、縣三級聯動
本文為大家分享一個基於開源控件 Android-wheel 實現省、市、縣三級聯動的完整示例,供大家參考。 GitHub 上有一個名為 Android-wheel 的開源