成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專(zhuān)欄INFORMATION COLUMN

Dialog使用詳解

Jenny_Tong / 3535人閱讀

摘要:對(duì)話(huà)框是提示用戶(hù)作出決定或輸入額外信息的小窗口。對(duì)話(huà)框不會(huì)填充屏幕,通常用于需要用戶(hù)采取行動(dòng)才能繼續(xù)執(zhí)行的模式事件。簡(jiǎn)介繼承關(guān)系如下基本樣式解析標(biāo)題這是可選項(xiàng),只應(yīng)在內(nèi)容區(qū)域被詳細(xì)消息列表或自定義布局占據(jù)時(shí)使用。

極力推薦文章:歡迎收藏
Android 干貨分享

閱讀五分鐘,每日十點(diǎn),和您一起終身學(xué)習(xí),這里是程序員Android

本篇文章主要介紹 Android 開(kāi)發(fā)中的部分知識(shí)點(diǎn),通過(guò)閱讀本篇文章,您將收獲以下內(nèi)容:

簡(jiǎn)單對(duì)話(huà)框

多選按鈕對(duì)話(huà)框

單選按鈕對(duì)話(huà)框

列表對(duì)話(huà)框

水平進(jìn)度條對(duì)話(huà)框

圓形進(jìn)度條對(duì)話(huà)框

自定義圖文對(duì)話(huà)框

自定義輸入對(duì)話(huà)框

自定義樣式對(duì)話(huà)框

自定義Loading樣式對(duì)話(huà)框

繼承 DialogFragment 實(shí)現(xiàn)對(duì)話(huà)框

Activity形式的 對(duì)話(huà)框

倒計(jì)時(shí) 30s Dialog實(shí)現(xiàn)

DialogAndroid 常用控件之一,主要以彈出框的形式與用戶(hù)進(jìn)行交互。對(duì)話(huà)框是提示用戶(hù)作出決定或輸入額外信息的小窗口。 對(duì)話(huà)框不會(huì)填充屏幕,通常用于需要用戶(hù)采取行動(dòng)才能繼續(xù)執(zhí)行的模式事件。

Dialog 簡(jiǎn)介 Dialog 繼承關(guān)系如下:
java.lang.Object
   ?    android.app.Dialog
Dialog 基本樣式解析

1.標(biāo)題

這是可選項(xiàng),只應(yīng)在內(nèi)容區(qū)域被詳細(xì)消息、列表或自定義布局占據(jù)時(shí)使用。 如需陳述的是一條簡(jiǎn)單消息或問(wèn)題(如圖 上圖中的對(duì)話(huà)框),則不需要標(biāo)題。

2.內(nèi)容區(qū)域

它可以顯示消息、列表或其他自定義布局。

3.操作按鈕

對(duì)話(huà)框中的操作按鈕不應(yīng)超過(guò)三個(gè)。

1. 簡(jiǎn)單對(duì)話(huà)框

實(shí)現(xiàn)效果:

實(shí)現(xiàn)代碼如下:

    /**
     * 簡(jiǎn)單對(duì)話(huà)框
     */
    public void SimpleDialog(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.gril).setTitle("簡(jiǎn)單對(duì)話(huà)框")
                .setMessage("設(shè)置Dialog 顯示的內(nèi)容")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(DiaLogMethods.this, "點(diǎn)擊了確定按鈕",
                                Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("Cancle", null).create().show();

    }
2. 多選按鈕對(duì)話(huà)框

實(shí)現(xiàn)效果:

實(shí)現(xiàn)代碼:

/**
     * 多選按鈕對(duì)話(huà)框
     * */
    public void MultiChoiceDialog(View view) {
        final String font[] = { "小號(hào)字體", "中號(hào)字體", "大號(hào)字體", "超大號(hào)字體" };
        final boolean[] MultiChoice = new boolean[] { false, true, false, false };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("多選對(duì)話(huà)框")
                .setIcon(R.drawable.ic_launcher)
                .setMultiChoiceItems(font, MultiChoice,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which, boolean isChecked) {
                                MultiChoice[which] = isChecked;
                                String choiceString = "";
                                for (int i = 0; i < MultiChoice.length; i++) {
                                    if (MultiChoice[i]) {
                                        choiceString = choiceString + font[i]
                                                + "  ";
                                    }
                                }

                                if (choiceString.equals("")
                                        || choiceString.length() == 0) {

                                    // 都不選的處理方法

                                    Toast.makeText(DiaLogMethods.this,
                                            "請(qǐng)選擇一個(gè)內(nèi)容", Toast.LENGTH_SHORT)
                                            .show();
                                } else {

                                    Toast.makeText(DiaLogMethods.this,
                                            "選擇的字體為" + choiceString,
                                            Toast.LENGTH_SHORT).show();

                                }

                            }
                        }).setPositiveButton("OK", null)
                .setNegativeButton("Cancle", null).create().show();

    }
3.單選按鈕對(duì)話(huà)框

實(shí)現(xiàn)效果:

實(shí)現(xiàn)代碼如下:

    /**
     * 單選按鈕對(duì)話(huà)框?qū)崿F(xiàn)
     **/
    public void SingleChoiceDialog(View view) {
        final String font[] = { "小號(hào)字體", "中號(hào)字體", "大號(hào)字體", "超大號(hào)字體" };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("單選對(duì)話(huà)框")
                .setIcon(R.drawable.ic_launcher)
                .setSingleChoiceItems(font, 0,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                Toast.makeText(DiaLogMethods.this,
                                        "選擇的字體為:" + font[which],
                                        Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        }).setPositiveButton("OK", null)
                .setNegativeButton("Cancle", null).create().show();

    }
4. 列表對(duì)話(huà)框

實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)代碼如下:

/**
     * 列表對(duì)話(huà)框?qū)崿F(xiàn)
     **/
    public void ListItemDialog(View view) {
        final String font[] = { "小號(hào)字體", "中號(hào)字體", "大號(hào)字體", "超大號(hào)字體" };
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.ic_launcher)
                .setTitle(" 列表對(duì)話(huà)框")
                .setItems(font, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DiaLogMethods.this,
                                "選擇內(nèi)容是:" + font[which], Toast.LENGTH_SHORT)
                                .show();
                    }
                }).setNegativeButton("Cancle", null)
                .setPositiveButton("OK", null).create().show();

    }
5. 水平進(jìn)度條對(duì)話(huà)框

實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)代碼如下:

    /**
     * 水平進(jìn)度條對(duì)話(huà)框?qū)崿F(xiàn)
     **/
    @SuppressWarnings("deprecation")
    public void HorProgressDialog(View view) {

        final ProgressDialog progressDialog = new ProgressDialog(
                DiaLogMethods.this);
        progressDialog.setTitle("進(jìn)度對(duì)話(huà)框");
        progressDialog.setIcon(R.drawable.ic_launcher);
        progressDialog.setMessage("加載中...");
        // 水平進(jìn)度條顯示
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 圓形進(jìn)度條顯示
        // progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
        progressDialog.setButton("Cancle",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(DiaLogMethods.this, "取消進(jìn)度條對(duì)話(huà)框",
                                Toast.LENGTH_LONG).show();
                        progressDialog.cancel();
                        count = 0;
                    }
                });
        progressDialog.setMax(100);
        progressDialog.show();
        count = 0;
        new Thread() {
            @Override
            public void run() {

                while (count <= 100) {
                    progressDialog.setProgress(count++);
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        progressDialog.dismiss();
                        e.printStackTrace();
                    }

                }
                progressDialog.dismiss();
            }
        }.start();

    }
6. 圓形進(jìn)度條對(duì)話(huà)框

實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)代碼如下:

/**
     * 圓形進(jìn)度條顯示
     **/
    @SuppressWarnings("deprecation")
    public void SpinerProgressDialog(View view) {

        final ProgressDialog progressDialog = new ProgressDialog(
                DiaLogMethods.this);
        progressDialog.setTitle("進(jìn)度對(duì)話(huà)框");
        progressDialog.setIcon(R.drawable.ic_launcher);
        progressDialog.setMessage("加載中...");
        // 水平進(jìn)度條顯示
        // progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 圓形進(jìn)度條顯示
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
        progressDialog.setButton("確定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(DiaLogMethods.this, "取消進(jìn)度條對(duì)話(huà)框",
                        Toast.LENGTH_LONG).show();
                progressDialog.cancel();
                count = 0;
            }
        });
        progressDialog.setMax(100);
        progressDialog.show();
        count = 0;
        new Thread() {
            @Override
            public void run() {

                while (count <= 100) {
                    progressDialog.setProgress(count++);
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        progressDialog.dismiss();
                        e.printStackTrace();
                    }

                }
                progressDialog.dismiss();
            }
        }.start();

    }
注意 :

水平進(jìn)度條,圓形進(jìn)度條的區(qū)別 如下:

        // 水平進(jìn)度條顯示
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // 圓形進(jìn)度條顯示
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
7. 自定義圖文對(duì)話(huà)框

實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)代碼如下:

    /**
     * 自定義圖文對(duì)話(huà)框?qū)崿F(xiàn)
     **/
    public void CustomImgTvDialog(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        View contextview = getLayoutInflater().inflate(
                R.layout.dialog_custom_img_tv, null);
        LinearLayout linearLayout1 = (LinearLayout) findViewById(R.id.linlout1);
        LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.linlout2);
        ImageView img1 = (ImageView) contextview.findViewById(R.id.img1);
        TextView tv1 = (TextView) contextview.findViewById(R.id.tv1);
        // 這里可以處理一些點(diǎn)擊事件

        builder.setIcon(R.drawable.gril).setTitle("自定義對(duì)話(huà)框")
                .setView(contextview)
                // 或者在這里處理一些事件
                .setPositiveButton("OK", null)
                .setNegativeButton("Cancle", null).create().show();
    }
注意:

自定義圖文對(duì)話(huà)框的布局如下:




    

        

        
    

    

    

        

        
    

8. 自定義輸入對(duì)話(huà)框

實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)代碼如下:

    /**
     * 自定義EditText對(duì)話(huà)框
     **/
    public void CustomEditTextDialog(View view) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this,
                android.R.style.Theme_Material_Light_Dialog_Alert);
        View Tittleview = getLayoutInflater().inflate(
                R.layout.dialog_custom_layout, null);
        ImageView img2 = (ImageView) Tittleview.findViewById(R.id.img2);
        TextView textView = (TextView) Tittleview.findViewById(R.id.tv2);

        textView.setText("自定義對(duì)話(huà)框");
        img2.setImageResource(R.drawable.ic_launcher);
        // 自定義tittle
        builder.setCustomTitle(Tittleview);

        View contentView = getLayoutInflater().inflate(
                R.layout.dialog_custom_et, null);
        EditText username = (EditText) contentView.findViewById(R.id.username);
        EditText passworld = (EditText) contentView
                .findViewById(R.id.passworld);

        builder.setView(contentView);
        builder.setPositiveButton("OK", null).setNegativeButton("Cancle", null)
                .create().show();

    }

自定義對(duì)話(huà)框 布局如下:




    

    

自定義 EditText 內(nèi)容布局




    

    

    

9. 自定義樣式對(duì)話(huà)框

實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)代碼如下:

    /**
     * 自定義樣式對(duì)話(huà)框
     * **/
    public void CustomStyleDialog(View v) {

        // 對(duì)話(huà)框和activity綁定,所以必須傳遞activity對(duì)象
        Builder builder = new AlertDialog.Builder(this,
                android.R.style.Theme_Material_Light_Dialog_Alert);
        // 獲取對(duì)話(huà)框?qū)ο?        final AlertDialog dialog = builder.create();
        // 修改對(duì)話(huà)框的樣式(布局結(jié)構(gòu))
        View view = View.inflate(this, R.layout.dialog_custom_style, null);

        // 因?yàn)樵?.3.3版本上,系統(tǒng)默認(rèn)設(shè)置內(nèi)間距,所以需要去除此內(nèi)間距
        // dialog.setView(view);
        dialog.setView(view, 0, 0, 0, 0);

        // 找到對(duì)話(huà)框中所有控件
        Button bt_submit = (Button) view.findViewById(R.id.bt_submit);
        Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);

        final EditText et_set_psd = (EditText) view
                .findViewById(R.id.et_set_psd);
        final EditText et_confirm_psd = (EditText) view
                .findViewById(R.id.et_confirm_psd);

        bt_submit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 如果用戶(hù)沒(méi)有輸入兩次密碼,告知用戶(hù)輸入密碼
                String psd = et_set_psd.getText().toString().trim();
                String confirmPsd = et_confirm_psd.getText().toString().trim();
                if (!TextUtils.isEmpty(psd) && !TextUtils.isEmpty(confirmPsd)) {
                    if (psd.equals(confirmPsd)) {
                        // 當(dāng)前的對(duì)話(huà)框隱藏
                        dialog.dismiss();

                    } else {
                        Toast.makeText(getApplicationContext(), "兩次輸入密碼不一致",
                                Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getApplicationContext(), "密碼不能為空",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });

        bt_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        // 展示對(duì)話(huà)框
        dialog.show();

    }
1. 自定義樣式dialog_custom_style布局如下:

dialog_custom_style 布局




    

    

    

    

    

    

        

        
    

2. EditText 的背景是畫(huà)的edittext_background 圓角矩形

edittext_background 實(shí)現(xiàn)




    
    
    
    

android.R.style.Theme_Material_Light_Dialog_Alert 是用來(lái)定義Dialog 樣式。

        Builder builder = new AlertDialog.Builder(this,
                android.R.style.Theme_Material_Light_Dialog_Alert);
10. 自定義Loading樣式對(duì)話(huà)框

實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)代碼如下:

    /**
     * 自定義Loading樣式對(duì)話(huà)框
     ***/
    public void CustomStyleProgressDialog(View view) {

        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(R.layout.dialog_custom_style_progress, null);
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);

        ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);
        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);

        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this,
                R.anim.loading_animation);

        spaceshipImage.startAnimation(hyperspaceJumpAnimation);

        Dialog loadingDialog = new Dialog(this, R.style.loading_dialog);

        // loadingDialog.setCancelable(true);//“返回鍵”取消 不可以用
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        loadingDialog.show();
    }
1. 自定義Dialog Sstyle 如下:
    
    
2. 自定義Dialog 樣式動(dòng)畫(huà)如下:



    
    

3. 自定義樣式的布局如下:



    

    

11. 繼承 DialogFragment 實(shí)現(xiàn)對(duì)話(huà)框

實(shí)現(xiàn)效果如下:

1.自定義繼承DialogFragment 類(lèi)

實(shí)現(xiàn)代碼如下:

自定義繼承DialogFragment 類(lèi)

public class CustomDialogFragment 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("通過(guò) DialogFragment 創(chuàng)建對(duì)話(huà)框")
                .setTitle("DialogFragment")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Toast.makeText(getActivity(), "點(diǎn)擊 OK",
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("cancle",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                // User cancelled the dialog
                            }
                        });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}
2. Activity 調(diào)用顯示Dialog方法
    /**
     * 繼承 DialogFragment 實(shí)現(xiàn)對(duì)話(huà)框
     * **/
    public void CustomFragmentDialog(View view) {

        CustomDialogFragment customDialogFragment = new CustomDialogFragment();
        customDialogFragment.show(getFragmentManager(), "fragment");
    }
12. Activity形式的 對(duì)話(huà)框

只需創(chuàng)建一個(gè) Activity,并在 清單文件元素中將其主題設(shè)置為 Theme.Holo.Dialog樣式即可

13.倒計(jì)時(shí) 30s Dialog實(shí)現(xiàn)

實(shí)現(xiàn)效果如下:

實(shí)現(xiàn)代碼如下:

    private TextView mShutDownTextView;
    private Handler mOffHandler;
    private Timer mShutdownTime;
    private Dialog mDialog;

    public void CountDownDialog(View view) {

        CreateShutDownDialog();

    }

    private Handler mNumHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what > 0) {

                // //動(dòng)態(tài)顯示倒計(jì)時(shí)
                mShutDownTextView
                        .setText("Warning! Battery temperature°С, phone will shutdown in "
                                + msg.what + "s");

            } else {
                if (mDialog != null) {
                    mDialog.dismiss();
                }
                mShutdownTime.cancel();
                Toast.makeText(getApplicationContext(), "倒計(jì)時(shí)結(jié)束", 0).show();

            }
        }

    };

    private void CreateShutDownDialog() {

        mShutDownTextView = new TextView(this);
        mShutDownTextView.setLineSpacing(1.2f, 1.2f);
        mShutDownTextView.setText("");
        mShutDownTextView.setPadding(20, 20, 20, 20);
        mDialog = new AlertDialog.Builder(this).setTitle("Safety Warning")
                .setCancelable(false).setView(mShutDownTextView)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mShutdownTime.cancel();

                    }
                }).create();
        mDialog.show();
        mDialog.setCanceledOnTouchOutside(false);
        mShutdownTime = new Timer(true);
        TimerTask timeTask = new TimerTask() {
            int countTime = 30;

            public void run() {
                if (countTime > 0) {
                    countTime--;
                }
                Message msg = new Message();
                msg.what = countTime;
                mNumHandler.sendMessage(msg);
            }
        };
        mShutdownTime.schedule(timeTask, 1000, 1000);
    }

至此,本篇已結(jié)束,如有不對(duì)的地方,歡迎您的建議與指正。同時(shí)期待您的關(guān)注,感謝您的閱讀,謝謝!

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/75848.html

相關(guān)文章

  • CSS Modules詳解及React中實(shí)踐

    摘要:上例中打印的結(jié)果是對(duì)中的名都做了處理,使用對(duì)象來(lái)保存原和混淆后的對(duì)應(yīng)關(guān)系。結(jié)合實(shí)踐在處直接使用中名即可。如因?yàn)橹粫?huì)轉(zhuǎn)變類(lèi)選擇器,所以這里的屬性選擇器不需要添加。 showImg(http://gtms01.alicdn.com/tps/i1/TB15w0HLpXXXXbdaXXXjhvsIVXX-600-364.png); CSS 是前端領(lǐng)域中進(jìn)化最慢的一塊。由于 ES2015/201...

    wemall 評(píng)論0 收藏0
  • 四年來(lái)Android面試大綱,作為一個(gè)Android程序員

    摘要:再附一部分架構(gòu)面試視頻講解本文已被開(kāi)源項(xiàng)目學(xué)習(xí)筆記總結(jié)移動(dòng)架構(gòu)視頻大廠(chǎng)面試真題項(xiàng)目實(shí)戰(zhàn)源碼收錄 Java反射(一)Java反射(二)Java反射(三)Java注解Java IO(一)Java IO(二)RandomAccessFileJava NIOJava異常詳解Java抽象類(lèi)和接口的區(qū)別Java深拷貝和淺拷...

    不知名網(wǎng)友 評(píng)論0 收藏0
  • PopupWindow 使用詳解

    摘要:在經(jīng)常使用,效果跟效果類(lèi)似,不同點(diǎn)在于可以控制顯示的位置,比如底部顯示等。至此,本篇已結(jié)束,如有不對(duì)的地方,歡迎您的建議與指正。同時(shí)期待您的關(guān)注,感謝您的閱讀,謝謝 showImg(https://segmentfault.com/img/remote/1460000019975019?w=157&h=54); 極力推薦文章:歡迎收藏Android 干貨分享 showImg(http...

    huaixiaoz 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<