1. 程式人生 > >andrdroid基礎---對話方塊:Dialog、AlertDialog

andrdroid基礎---對話方塊:Dialog、AlertDialog

1.確定取消對話方塊

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

builder.setIcon(R.drawable.icon);

builder.setTitle("你確定要離開嗎?");

builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        //這裡新增點選確定後的邏輯

        showDialog("你選擇了確定");

    }

});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        //這裡新增點選確定後的邏輯

        showDialog("你選擇了取消");

    }

});

builder.create().show();

private void showDialog

(String str) {

    new AlertDialog.Builder(MainDialog.this).setMessage(str).show();

}

----------------------------------------------------------------------------------------------------------

 2.多個按鈕資訊框

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

builder.setIcon(R.drawable.icon);

builder.setTitle("投票");

builder.setMessage("您認為什麼樣的內容能吸引您?");

builder.setPositiveButton("有趣味的", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        showDialog("你選擇了有趣味的");

    }

});

builder.setNeutralButton("有思想的", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        showDialog("你選擇了有思想的");

    }

});

builder.setNegativeButton("主題強的", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        showDialog("你選擇了主題強的");

    }

});

builder.create().show();

 3.列表框

final String[] mItems = {"item0","item1","itme2","item3","itme4","item5","item6"};

[java] view plaincopy

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

builder.setTitle("列表選擇框");

builder.setItems(mItems, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int which) {

        //點選後彈出視窗選擇了第幾項

        showDialog("你選擇的id為" + which + " , " + mItems[which]);

    }

});

builder.create().show();

4.單項選擇列表框

int mSingleChoiceID = -1;

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

mSingleChoiceID = -1;

builder.setIcon(R.drawable.icon);

builder.setTitle("單項選擇");

builder.setSingleChoiceItems(mItems, 0, new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        mSingleChoiceID = whichButton;

        showDialog("你選擇的id為" + whichButton + " , " + mItems[whichButton]);

    }

});

builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        if(mSingleChoiceID > 0) {

            showDialog("你選擇的是" + mSingleChoiceID);

        }

    }

});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

    }

});

builder.create().show();

 

 5.進度條框

mProgressDialog = new ProgressDialog(MainDialog.this);

mProgressDialog.setIcon(R.drawable.icon);

mProgressDialog.setTitle("進度條視窗");

mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

mProgressDialog.setMax(MAX_PROGRESS);

mProgressDialog.setButton("確定", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        //這裡新增點選後的邏輯

    }

});

mProgressDialog.setButton2("取消", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        //這裡新增點選後的邏輯

    }

});

mProgressDialog.show();

new Thread(this).start();

public void run() {

    int Progress = 0;

    while(Progress < MAX_PROGRESS) {

        try {

            Thread.sleep(100);

            Progress++;

            mProgressDialog.incrementProgressBy(1);

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

}

 6.多項選擇列表框

ArrayListMultiChoiceID = new ArrayList();

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

MultiChoiceID.clear();

builder.setIcon(R.drawable.icon);

builder.setTitle("多項選擇");

builder.setMultiChoiceItems(mItems,

new boolean[]{false, false, false, false, false, false, false},

new DialogInterface.OnMultiChoiceClickListener() {

    public void onClick(DialogInterface dialog, int whichButton,boolean isChecked) {

        if(isChecked) {

            MultiChoiceID.add(whichButton);

            showDialog("你選擇的id為" + whichButton + " , " + mItems[whichButton]);

        }else {

             MultiChoiceID.remove(whichButton);

        }

    }

});

builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        String str = "";

        int size = MultiChoiceID.size();

        for (int i = 0 ;i < size; i++) {

        str+= mItems[MultiChoiceID.get(i)] + ", ";

        }

        showDialog("你選擇的是" + str);

    }

});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

    }

});

builder.create().show();

 7.自定義佈局

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

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

LayoutInflater factory = LayoutInflater.from(this);

final View textEntryView = factory.inflate(R.layout.test, null);

builder.setIcon(R.drawable.icon);

builder.setTitle("自定義輸入框");

builder.setView(textEntryView);

builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

        EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);

        EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);

        showDialog("姓名 :" + userName.getText().toString() + "密碼:" + password.getText().toString() );

    }

});

builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog, int whichButton) {

    }

});

builder.create().show();

8.讀取進度框

mProgressDialog = new ProgressDialog(this);

mProgressDialog.setTitle("讀取ing");

mProgressDialog.setMessage("正在讀取中請稍候");

mProgressDialog.setIndeterminate(true);

mProgressDialog.setCancelable(true);

mProgressDialog.show();