Android的单选、多选、进度条对话框

2019-10-06  本文已影响0人  小李同学今天博学了吗

    页面效果只上传了多选的对话框

多选对话框

public class MainActivityextends AppCompatActivity {

@Override

    protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

// 创建普通对话框,对话框中的上下文对象只能是当前对象this,在当前页面显示

    public void commandDialog(View view) {

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

.setTitle("警告")

.setTitle("你要卸载这个应用吗?")

.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override

                    public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(getApplicationContext(),"应用已完成卸载",Toast.LENGTH_SHORT).show();

                    }

})

.setNegativeButton("取消",null);

                builder.show();

    }

// 设置单选对话框

    public void singleDialog(View view) {

final String data[] = {"Android","IOS","VR","后端","前端","UI","游戏"};

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

.setTitle("请选择你想学习的方向")

.setSingleChoiceItems(data, -1,new DialogInterface.OnClickListener() {

@Override

                public void onClick(DialogInterface dialogInterface, int i) {

String choices =data[i];

                        Toast.makeText(getApplicationContext(),"恭喜你选择了"+choices,Toast.LENGTH_SHORT).show();

                        dialogInterface.dismiss();

                }

});

        builder.show();

    }

// 多选对话框调用的方法,第一个数组是传入的数据,第二个为判断当前的条目是否被选中的标志

    public void multipleDialog(View view) {

final String data[] = {"香蕉","苹果","西瓜","柠檬","火龙果","葡萄","山竹","🍑"};

        final boolean checkitem[] = {true,false,true,false,false,false,true,false};

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

.setTitle("请选择你喜欢吃的水果")

.setMultiChoiceItems(data, checkitem, new DialogInterface.OnMultiChoiceClickListener() {

@Override

                    public void onClick(DialogInterface dialogInterface, int i, boolean b) {

}

});

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

@Override

            public void onClick(DialogInterface dialogInterface, int i) {

StringBuffer buffer =new StringBuffer();

                for(int j=0;j

if(checkitem[j]){

buffer.append(data[j]+"    ");

                    }

}

Toast.makeText(getApplicationContext(),buffer.toString(),Toast.LENGTH_SHORT).show();

            }

});

        builder.show();

    }

// 进度条对话框

    public void progressDialogClick(View view) {

final ProgressBar progressBar =new ProgressBar(this);

        new Thread(new Runnable() {

@Override

            public void run() {

int total =100;

                progressBar.setMax(total);

                for(int i=0;i

progressBar.setProgress(i);

                    SystemClock.sleep(50);

                }

}

}).start();

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

.setView(progressBar);

        builder.show();

    }

}

上一篇 下一篇

猜你喜欢

热点阅读