android進度對話方塊的使用,android進度對話方塊
布局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".DialogActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text" /> <Button android:id="@+id/btn_dialog" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="多選對話方塊" /> <Button android:id="@+id/btn_dialog1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick1" android:text="進度對話方塊" /> <Button android:id="@+id/btn_dialog2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick2" android:text="詳細進度對話方塊" /> </LinearLayout>
DialogActivity.java
package com.dialog; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class DialogActivity extends Activity { //CharSequence介面 CharSequence[] items = {"Google","蘋果","百度","微軟"}; boolean[] itemsChecked = new boolean[items.length]; //建立ProgressDialog類的一個執行個體 ProgressDialog progressDialog = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } //重寫onCreateDialog()方法 protected Dialog onCreateDialog(int id, Bundle args) { switch (id) { case 0: /* * AlertDialog Dialog的子類 顯示進度列指示器和可選的簡訊或視圖的對話方塊 * 多選對話方塊 */ Builder builder = new AlertDialog.Builder(this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("你喜歡哪家互連網公司"); //響應確定按鈕 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { //Toast並沒有在DialogActivity直接使用,而是在AlertDialog類中使用 因此需要用getBaseContext()返回一個Context類的執行個體 public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "確定按鈕被點擊", Toast.LENGTH_SHORT).show(); } }); //響應取消按鈕 builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton) { Toast.makeText(getBaseContext(), "取消按鈕被點擊", Toast.LENGTH_SHORT).show(); } }); //響應多選事件 builder.setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { Toast.makeText(getBaseContext(), (isChecked?"選中":"未選中")+items[which], Toast.LENGTH_SHORT).show(); } }); return builder.create(); case 1: /* * 設定表徵圖 標題 樣式 按鈕 * 顯示詳細進度對話方塊 */ progressDialog = new ProgressDialog(this); progressDialog.setIcon(R.drawable.ic_launcher); progressDialog.setTitle("下載檔案..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show(); } }); progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getBaseContext(), "Cancel clicked!", Toast.LENGTH_SHORT).show(); } }); return progressDialog; } return null; } public void onClick(View v){ //調用onCreateDialog()方法 showDialog(0); } /* * 進度對話方塊 */ public void onClick1(View v){ final ProgressDialog dialog = ProgressDialog.show(this, "請等待3秒", "Please wait...",true); //啟動一個新線程 new Thread(new Runnable() { public void run() { try { Thread.sleep(3000); //解除對話方塊 從螢幕上移除 此方法是安全執行緒的 dialog.dismiss(); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } /* * 詳細進度對話方塊 */ public void onClick2(View v){ showDialog(1); //設定初始值為0 progressDialog.setProgress(0); new Thread(new Runnable() { public void run(){ for (int i = 1; i <= 15; i++) { try { Thread.sleep(1000); //更新對話方塊 progressDialog.incrementProgressBy((int)100/15); } catch (InterruptedException e) { e.printStackTrace(); } } progressDialog.dismiss(); } }).start(); } }
Android開發中,對於對話方塊中的圓形進度條與Layout的問題
首先解決問題一的問題:
你肯定使用了系統的 oncreateDialog 和 showdialog 方法了,所以 這個就會顯示一次 第二次不顯示了 , 你應該調用 dialog方法裡面的 show()方法 來顯示,這樣每次顯示那個progressbar都會在轉圈
問題二:
你說下面設定了一個白色背景,但是下面還是有個框,不過我有個疑問,如果你設定為白色背景,那麼你的轉條預設是白色的不就看不見了嗎?好了這個問題不糾結了!那個黑色的是邊框導致的,去掉邊框就行了
一般我們採用的是自訂dialog,也就是寫一個類來繼承dialog,這個時候的建構函式是這個:
public MyDialog(Context context, int theme) {
super(context, theme);
this.context = context;
init();
}
這個theme是什麼東西呢? 就是一個style樣式 如下:
<resources> <style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item><!--邊框-- <item name="android:windowIsFloating">true</item><!--是否浮現在activity之上--
<item name="android:windowIsTranslucent">false</item><!--半透明-- <item name="android:windowNoTitle">true</item><!--無標題-- <item name="android:windowBackground">@color/transparent</item><!--背景透明-- <item name="android:backgroundDimEnabled">false</item><!--模糊-- </style></resources>......餘下全文>>
android對話方塊加“確定”,“取消”兩個按鈕,卻總只顯示一個為何?
protected void dialog() {
AlertDialog.Builder builder = new Builder(Main.this);
builder.setMessage("確認退出嗎?");
builder.setTitle("提示");
builder.setPositiveButton("確認", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Main.this.finish();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
你的連個都是 builder.setPositiveButton()當然只會顯示一個了 呵呵