幾種基本對(duì)話框,AlertDialog

九種對(duì)話框.png

基本的 alertdialog 使用

public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonNormal = (Button) findViewById(R.id.button_normal);
        buttonNormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNormalDialog();
            }
        });
    }
     
    private void showNormalDialog(){
        /* @setIcon 設(shè)置對(duì)話框圖標(biāo)
         * @setTitle 設(shè)置對(duì)話框標(biāo)題
         * @setMessage 設(shè)置對(duì)話框消息提示
         * setXXX方法返回Dialog對(duì)象,因此可以鏈?zhǔn)皆O(shè)置屬性
         */
        final AlertDialog.Builder normalDialog = 
            new AlertDialog.Builder(MainActivity.this);
        normalDialog.setIcon(R.drawable.icon_dialog);
        normalDialog.setTitle("我是一個(gè)普通Dialog")
        normalDialog.setMessage("你要點(diǎn)擊哪一個(gè)按鈕呢?");
        normalDialog.setPositiveButton("確定", 
            new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //...To-do
            }
        });
        normalDialog.setNegativeButton("關(guān)閉", 
            new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //...To-do
            }
        });
        // 顯示
        normalDialog.show();
    }
}

3 個(gè)按鈕

/* @setNeutralButton 設(shè)置中間的按鈕
 * 若只需一個(gè)按鈕,僅設(shè)置 setPositiveButton 即可
 */
private void showMultiBtnDialog(){
    AlertDialog.Builder normalDialog = 
        new AlertDialog.Builder(MainActivity.this);
    normalDialog.setIcon(R.drawable.icon_dialog);
    normalDialog.setTitle("我是一個(gè)普通Dialog").setMessage("你要點(diǎn)擊哪一個(gè)按鈕呢?");
    normalDialog.setPositiveButton("按鈕1", 
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...To-do
        }
    });
    normalDialog.setNeutralButton("按鈕2", 
        new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...To-do
        }
    });
    normalDialog.setNegativeButton("按鈕3", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // ...To-do
        }
    });
    // 創(chuàng)建實(shí)例并顯示
    normalDialog.show();
}

參考文章
android 8 種對(duì)話框(Dialog)使用方法匯總 - 呆尐兔兔 - 博客園
https://www.cnblogs.com/gzdaijie/p/5222191.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容