android基础---对话框: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();

}

android基础---对话框:Dialog、AlertDialog_第1张图片

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

 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();

android基础---对话框:Dialog、AlertDialog_第2张图片

 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();

android基础---对话框:Dialog、AlertDialog_第3张图片

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();

 android基础---对话框:Dialog、AlertDialog_第4张图片

 

 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();

        }

}

android基础---对话框:Dialog、AlertDialog_第5张图片

 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();

android基础---对话框:Dialog、AlertDialog_第6张图片

 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();

android基础---对话框:Dialog、AlertDialog_第7张图片

8.读取进度框

mProgressDialog = new ProgressDialog(this);

mProgressDialog.setTitle("读取ing");

mProgressDialog.setMessage("正在读取中请稍候");

mProgressDialog.setIndeterminate(true);

mProgressDialog.setCancelable(true);

mProgressDialog.show();

 

下载地址:http://vdisk.weibo.com/s/a9iiE

  • 本文固定链接: https://www.xuanyusong.com/archives/9
  • 转载请注明: 雨松MOMO 2012年04月25日 于 雨松MOMO程序研究院 发表

你可能感兴趣的:(Android,android,dialog,alertDialog)