Dialog各种各种

MainActivity

public class MainActivity extends AppCompatActivity {
    ProgressDialog progressDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    String name="我是张三";
    public void click(View view){
        switch (view.getId()){
            case R.id.btn1://信息提示框
                new AlertDialog.Builder(this)//在本页面创建出一个弹出框
                .setTitle("标题")//设置标题
                .setIcon(R.mipmap.ic_launcher)//设置标题的图片
                .setMessage(name).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
                    }
                }).show();//show():弹出Dialig
                break;
            case R.id.btn2://确认框
                new AlertDialog.Builder(this)//在本页面创建出一个弹出框
                        .setTitle("标题")//设置标题
                        .setIcon(R.mipmap.ic_launcher)//设置标题的图片
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //在这做删除的工作
                                Toast.makeText(MainActivity.this,"信息已删除",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消",null)//取消按钮设置
                        .show();
                break;
            case R.id.btn3://单选框
                final String[] arr={"牛蛙","田鸡","蛤蟆","蝎子"};
                new AlertDialog.Builder(this)//在本页面创建出一个弹出框
                        .setTitle("标题")//设置标题
                        .setIcon(R.mipmap.ic_launcher)//设置标题的图片
                        .setSingleChoiceItems(arr, 1, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                Toast.makeText(MainActivity.this,arr[which],Toast.LENGTH_SHORT).show();
                            }
                        }).show();
                break;
            case R.id.btn4://多选框
                final String[] arr1={"牛蛙","田鸡","蛤蟆","蝎子"};
                final StringBuilder sb=new StringBuilder();
                new AlertDialog.Builder(this)//在本页面创建出一个弹出框
                        .setTitle("标题")//设置标题
                        .setIcon(R.mipmap.ic_launcher)//设置标题的图片
                        .setMultiChoiceItems(arr1, null, new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                                //判断当前选项是否是选中状态,若是,就取出值放到sb,如果不选中,就把当前值从sb里删除掉
                                if(isChecked){
                                    sb.append(arr1[which]);
                                }else {
                                    int start=sb.indexOf(arr1[which]);
                                    sb.delete(start,start+arr1[which].length());
                                }
                            }
                        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        Toast.makeText(MainActivity.this,sb.toString(),Toast.LENGTH_SHORT).show();
                    }
                }).show();
                break;
            case R.id.btn5://进度框
                final ProgressDialog progressDialog = new ProgressDialog(this);//进度提示框
                progressDialog.setTitle("下载进度");
                progressDialog.setIcon(R.mipmap.ic_launcher);
                progressDialog.setMessage("当前正在下载中……");
                progressDialog.setMax(100);
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置为横的进度条
                progressDialog.show();
                final Handler han=new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        if(msg.what==110){
                            progressDialog.setProgress(msg.arg1);
                            if(msg.arg1==100){
                                progressDialog.dismiss();
                            }
                        }

                    }
                };
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i=0;i<=100;i+=10){
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            Message mess=Message.obtain();
                            mess.arg1=i;
                            mess.what=110;
                            han.sendMessage(mess);
                        }
                    }
                }).start();
                break;
        }
    }
}
activity布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="org.frxm.day20_dialog.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="信息提示框"
        android:onClick="click"/>
    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确认框"
        android:onClick="click"/>
    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选框"
        android:onClick="click"/>
    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多选框"
        android:onClick="click"/>
    <Button
        android:id="@+id/btn5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="进度框"
        android:onClick="click"/>


</LinearLayout>



阅读更多
换一批

没有更多推荐了,返回首页