Android開(kāi)發(fā)-AlertDialog阶牍,Progress喷面,ProgressDialog,自定義Dialog

AlertDialog

  • 默認(rèn)樣式
  • 單選樣式
  • 多選樣式
  • 自定義樣式

效果圖

AlertDialog效果圖
class OnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.btn_dialog1:
                    AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                    builder.setTitle("請(qǐng)回答").setMessage("你覺(jué)得課程如何")
                            .setIcon(R.drawable.user)
                            .setPositiveButton("棒", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ToastUtil.showShort(DialogActivity.this, "很誠(chéng)實(shí)");
                        }
                    }).setNeutralButton("還行", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ToastUtil.showShort(DialogActivity.this, "再瞅瞅");
                        }
                    }).setNegativeButton("不好", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ToastUtil.showShort(DialogActivity.this, "睜眼說(shuō)瞎話");
                        }
                    }).show();
                    break;
                case R.id.btn_dialog2:

                    final String[] array = new String[]{"男", "女"};
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
                    builder1.setTitle("選擇性別").setItems(array, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ToastUtil.showLong(DialogActivity.this, array[which]);
                        }
                    }).show();

                    break;
                case R.id.btn_dialog3:

                    final String[] array3 = new String[]{"男", "女"};
                    AlertDialog.Builder builder3 = new AlertDialog.Builder(DialogActivity.this);
                    builder3.setTitle("選擇性別").setSingleChoiceItems(array3, 0, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            ToastUtil.showLong(DialogActivity.this, array3[which]);
                            dialog.dismiss();
                        }
                    }).setCancelable(false).show();
                    break;
                case R.id.btn_dialog4:

                    final String[] array4 = new String[]{"唱歌", "跳舞", "寫代碼"};
                    boolean[] isSelected = new boolean[]{false, false, false};
                    AlertDialog.Builder builder4 = new AlertDialog.Builder(DialogActivity.this);
                    builder4.setTitle("選擇興趣").setMultiChoiceItems(array4, isSelected, new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                            ToastUtil.showLong(DialogActivity.this, array4[which] + ":" + isChecked);
                        }
                    }).setPositiveButton("確定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    }).show();
                    break;
                case R.id.btn_dialog5:

                    AlertDialog.Builder builder5 = new AlertDialog.Builder(DialogActivity.this);
                    View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.layout_dialog, null);
                    EditText etUserName = view.findViewById(R.id.et_username);
                    EditText etPassWord = view.findViewById(R.id.et_password);
                    Button button = view.findViewById(R.id.et_login);
                    button.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                        }
                    });
                    builder5.setTitle("請(qǐng)先登錄").setView(view).show();
                    break;
            }
        }
    }

自定義樣式layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="username"
        android:maxLines="1"/>

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="password"
        android:inputType="textPassword"
        android:maxLines="1"/>

    <Button
        android:id="@+id/et_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="login"
        android:textAllCaps="false"
        android:layout_marginTop="10dp"/>

</LinearLayout>

Progress

Progress效果圖

代碼
activity_progress.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ProgressActivity"
    android:orientation="vertical"
    android:gravity="center_horizontal">


    <ProgressBar
        android:id="@+id/pb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ProgressBar
        android:id="@+id/pb2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar"/>

    <ProgressBar
        android:id="@+id/pb3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:progress="40"
        android:max="200"
        android:secondaryProgress="80"
        />

    <ProgressBar
        android:id="@+id/pb4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.Material.ProgressBar.Horizontal"
        android:progress="40"
        android:max="200"
        android:secondaryProgress="80"
        />

    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:text="點(diǎn)擊"/>


    <ProgressBar
        android:id="@+id/pb5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/MyProgressBar"
        android:layout_marginTop="10dp"
        />
    <!--        android:indeterminateDrawable="@drawable/bg_progress"-->

    <Button
        android:id="@+id/btn_progress_dialog1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:layout_marginTop="10dp"
        android:text="ProgressDialog1"/>

    <Button
        android:id="@+id/btn_progress_dialog2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAllCaps="false"
        android:layout_marginTop="10dp"
        android:text="ProgressDialog2"/>
</LinearLayout>

ProgressActivity

public class ProgressActivity extends AppCompatActivity {

    private ProgressBar mPb3;
    private Button pbBtn, mBtnProgressDialog1, mBtnProgressDialog2;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        mPb3 = findViewById(R.id.pb3);
        mPb3.setProgress(90);

        mBtnProgressDialog1 = findViewById(R.id.btn_progress_dialog1);
        mBtnProgressDialog2 = findViewById(R.id.btn_progress_dialog2);

        pbBtn = findViewById(R.id.btn);
        pbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(0);
            }
        });
        mBtnProgressDialog1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
                progressDialog.setTitle("提示");
                progressDialog.setMessage("正在加載");
                progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialog) {
                        ToastUtil.showLong(ProgressActivity.this, "cancel");
                    }
                });
                progressDialog.setCancelable(false);
                progressDialog.show();
            }
        });

        mBtnProgressDialog2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ProgressDialog progressDialog = new ProgressDialog(ProgressActivity.this);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setTitle("提示");
                progressDialog.setMessage("正在下載...");
                progressDialog.setButton(BUTTON_POSITIVE, "棒", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                progressDialog.show();
            }
        });
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (mPb3.getProgress() < 200) {
                handler.postDelayed(runnable, 500);
            }else  {
                ToastUtil.showShort(ProgressActivity.this, "加載完成");
            }
        }
    };

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            mPb3.setProgress(mPb3.getProgress() + 5);
            handler.sendEmptyMessage(0);
        }
    };
}

自定義Dialog

自定義Dialog

layout_custom_dialog.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:background="@drawable/bg_custom_dialog"
    >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/colorBlack"
        android:text="提示"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@color/colorBlack"
        android:text="刪除?"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/colorGray"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_cancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="取消"
            android:textColor="#11c2ee"
            android:gravity="center"
            />

        <View
            android:layout_width="0.5dp"
            android:layout_height="match_parent"
            android:background="@color/colorGray"/>

        <TextView
            android:id="@+id/tv_confirm"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="確定"
            android:textColor="#000"
            android:gravity="center"
            />

    </LinearLayout>

</LinearLayout>

CustomDialogActivity

public class CustomDialogActivity extends AppCompatActivity {

    private Button mBtnDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_dialog);
        mBtnDialog = findViewById(R.id.btn_custom_dialog);
        mBtnDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CustomDialog customDialog = new CustomDialog(CustomDialogActivity.this, R.style.CustomDialog);
                customDialog.setTitle("提示").setMessage("確認(rèn)刪除此項(xiàng)走孽?").setCancel("取消",new CustomDialog.IOnCancelListener() {
                    @Override
                    public void onCancel(CustomDialog dialog) {

                    }
                }).setConfirm("確定1",new CustomDialog.IOnConfirmListener() {
                    @Override
                    public void onConfirm(CustomDialog dialog) {

                    }
                }).show();
            }
        });
    }
}

activity_dialog_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CustomDialogActivity">

    <Button
        android:id="@+id/btn_custom_dialog"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定義dialog"
        android:textAllCaps="false"
        />

</LinearLayout>

CustomDialog Style


    <style name="CustomDialog"
        parent="@android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowBackground">@color/colorAccent</item>
    </style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末乖酬,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子融求,更是在濱河造成了極大的恐慌咬像,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件生宛,死亡現(xiàn)場(chǎng)離奇詭異县昂,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)陷舅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門倒彰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人莱睁,你說(shuō)我怎么就攤上這事待讳。” “怎么了仰剿?”我有些...
    開(kāi)封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵创淡,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我南吮,道長(zhǎng)琳彩,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任部凑,我火速辦了婚禮露乏,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘涂邀。我一直安慰自己瘟仿,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開(kāi)白布比勉。 她就那樣靜靜地躺著劳较,像睡著了一般驹止。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上兴想,一...
    開(kāi)封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天,我揣著相機(jī)與錄音赡勘,去河邊找鬼嫂便。 笑死,一個(gè)胖子當(dāng)著我的面吹牛闸与,可吹牛的內(nèi)容都是我干的毙替。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼践樱,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼厂画!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起拷邢,我...
    開(kāi)封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤袱院,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后瞭稼,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體忽洛,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年环肘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了欲虚。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡悔雹,死狀恐怖复哆,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情腌零,我是刑警寧澤梯找,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站益涧,受9級(jí)特大地震影響初肉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜饰躲,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一牙咏、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嘹裂,春花似錦妄壶、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)氨淌。三九已至,卻和暖如春伊磺,著一層夾襖步出監(jiān)牢的瞬間盛正,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工屑埋, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留豪筝,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓摘能,卻偏偏與公主長(zhǎng)得像续崖,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子团搞,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354