Android學習筆記-Dialog詳解,android-dialog

來源:互聯網
上載者:User

Android學習筆記-Dialog詳解,android-dialog

1.對話方塊的使用

1.1AlertDialog的顯示

簡單對話方塊以及監聽的設定:重點掌握三個按鈕(也就是三上單詞):

PositiveButton(確認按鈕);NeutralButton(忽略按鈕)

AlertDialog.Builder bud1=new Builder(mContext);

bud1.setTitle("提示資訊");

bud1.setMessage("您的資訊已提交完成!");

bud1.setPositiveButton("確認", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss(); //對話方塊消失

}

});

bud1.setPositiveButton(" 取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();//取消對話方塊 Toast.makeText(mContext, "你已取消會話!", Toast.LENGTH_LONG).show();

}

});

bud1.setNeutralButton("忽略", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

Toast.makeText(Code06_03.this, "你已忽略。。。", Toast.LENGTH_LONG).show();

}

});

//選項按鈕,選取其中一個

//1.設定資料來源

final String[] items = {"android", "ipone", "Symbian"}; 

//2.資料適配

bud1.setItems(items, new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, int which) { 

         Toast.makeText(mContext,items[which],Toast.LENGTH_SHORT).show(); 

        } 

    });

 

 

//自訂對話方塊

  ImageView img = new ImageView(this);

     img.setImageResource(R.drawable.icon);

     Bud1

     .setView(img)

 

bud1.create().show();//顯示對話方塊

 

 

 

1.2.ProgressDialog的顯示

關鍵為什麼在子線程中調用UI操作未報錯問題,經過分析,

ProgressDialg內部已經實現了Hanlder,因而未報錯。而runOnUiThread的使用也顯的多餘了。

 

protected void dialog9() {

new Thread(new Runnable() {

@Override

public void run() {

try {

Thread.sleep(5 * 1000);

// progressDialog.dismiss();

runOnUiThread(finishDialog);

} catch (InterruptedException e) {

}

}

}).start();

progressDialog = ProgressDialog.show(Code06_07.this, "請稍等",

"資料正在載入中...", true);

}

 

private Runnable finishDialog = new Runnable() {

@Override

public void run() {

progressDialog.dismiss();

}

};

 

 

 

m_pDialog = new ProgressDialog(Code06_09.this);

// 設定進度條風格,風格為長形

m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

// 設定ProgressDialog 標題

m_pDialog.setTitle("提示");

// 設定ProgressDialog 提示資訊

m_pDialog.setMessage("這是一個長形對話方塊進度條");

// 設定ProgressDialog 標題表徵圖

m_pDialog.setIcon(R.drawable.icon);

 

// 設定ProgressDialog 進度條進度

m_pDialog.setProgress(100);

 

// 設定ProgressDialog 的進度條是否不明確

m_pDialog.setIndeterminate(false);

 

// 設定ProgressDialog 是否可以按退回按鍵取消

m_pDialog.setCancelable(true);

 

// 讓ProgressDialog顯示

m_pDialog.show();

 

new Thread() {

public void run() {

try {

while (m_count <= 100) {

// 由線程來控制進度。

m_pDialog.setProgress(m_count++);

Thread.sleep(1000);

}

m_pDialog.cancel();

} catch (InterruptedException e) {

m_pDialog.cancel();

}

}

}.start();

 

}

 

我在測試代碼時,遇到一個過時的方法,有一個新的API以供使用

 

 

/***
 * 第一個參數是用來確定哪個按鈕綁定監聽
 *  @param whichButton Which button to set the listener on, can be one of
 *  有以下常量來提供選擇
 *  BUTTON_POSITIVE
 *  {@link DialogInterface#BUTTON_POSITIVE}
 *  {@link DialogInterface#BUTTON_NEGATIVE}
 *  {@link DialogInterface#BUTTON_NEUTRAL
 **/
progress.setButton(AlertDialog.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int i) {
        // 點擊“確定按鈕”取消對話方塊
        dialog.cancel();
    }
});

經以上分析,與代碼的編寫,可以總結出:ProgressDialog的延時,使用了Handler機制,可以放心的在Thread中延時,在Thread中的操作有 setProgress,dismiss等方法。

1.3 自訂對話方塊-

LayoutInflater inflater=mContext.getLayoutInflater();

mView=inflater.inflate(R.layout.dialog,// 自訂對話方塊視圖

null);

 

AlertDialog.Builder(mContext).setView(mView);

 

飛哥今天講的知識點,就是關於兩個類的使用。ProgressDialog,AlertDialog這兩個類,主要講解了如何使用它們的方法,以及ProgressDialog在特性,在Thread中使用。

 

1.4AlertDialog解析

AlertDialog extens Dialog implents DialogInterface

三個子類:DataPicker,ProgressDialog,TimePickerDialog

接下來,看看APIGuida有什麼驚喜呢?

AlertDialog

A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

通過這句話,我知道了飛哥的不易,把AlertDialog的功能全用了一遍,the,I  Knowed  it,and did it 

DatePickerDialog or TimePickerDialog

A dialog with a pre-defined UI that allows the user to select a date or time.

一個提前定義好的對話方塊,允許使用者選擇時間或日期

 

 you should use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object

看到這句話,不知道你有何感想-感覺前面的代碼沒用了有木有,大Boss是這個DialogFragment呀!不要急,請看後面。

簡單的說:1.正確的處理Dialog的生命週期

  2.對話方塊的簡單利用,就像Fragment一樣,當成一個組件

          3.支援API層級泛圍廣Android1.6以上均支援,當然需要支援庫

這裡Google給出一個樣本,封裝AlertDialog,以複用

public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

當你擷取執行個體後,便可以show了

 

參考:

FragmentDialog:詳解:傳送門

Andorid SDK Dialog

擴充閱讀:

Full Screen DialogFragment (over ActionBar) in Android:傳送門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.