• 4.19


    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button btn_dialog_one;
        private Button btn_dialog_two;
        private Button btn_dialog_three;
        private Button btn_dialog_four;
    
        private Context mContext;
        private boolean[] checkItems;
    
        private AlertDialog alert = null;
        private AlertDialog.Builder builder = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mContext = MainActivity.this;
            bindView();
    
    
        }
    
        private void bindView() {
            btn_dialog_one = (Button) findViewById(R.id.btn_dialog_one);
            btn_dialog_two = (Button) findViewById(R.id.btn_dialog_two);
            btn_dialog_three = (Button) findViewById(R.id.btn_dialog_three);
            btn_dialog_four = (Button) findViewById(R.id.btn_dialog_four);
            btn_dialog_one.setOnClickListener(this);
            btn_dialog_two.setOnClickListener(this);
            btn_dialog_three.setOnClickListener(this);
            btn_dialog_four.setOnClickListener(this);
        }
    
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                //普通对话框
                case R.id.btn_dialog_one:
                    alert = null;
                    builder = new AlertDialog.Builder(mContext);
                    alert = builder.setIcon(R.mipmap.ic_icon_fish)
                            .setTitle("系统提示:")
                            .setMessage("这是一个最普通的AlertDialog,
    带有三个按钮,分别是取消,中立和确定")
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(mContext, "你点击了取消按钮~", Toast.LENGTH_SHORT).show();
                                }
                            })
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(mContext, "你点击了确定按钮~", Toast.LENGTH_SHORT).show();
                                }
                            })
                            .setNeutralButton("中立", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Toast.makeText(mContext, "你点击了中立按钮~", Toast.LENGTH_SHORT).show();
                                }
                            }).create();             //创建AlertDialog对象
                    alert.show();                    //显示对话框
                    break;
                //普通列表对话框
                case R.id.btn_dialog_two:
                    final String[] lesson = new String[]{"语文", "数学", "英语", "化学", "生物", "物理", "体育"};
                    alert = null;
                    builder = new AlertDialog.Builder(mContext);
                    alert = builder.setIcon(R.mipmap.ic_icon_fish).setTitle("选择你喜欢的课程").setItems(lesson,newDialogInterface.OnClickListener(){@Overridepublicvoid onClick(DialogInterface dialog,int which){Toast.makeText(getApplicationContext(),"你选择了"+ lesson[which],Toast.LENGTH_SHORT).show();}}).create();
                    alert.show();break;//单选列表对话框case R.id.btn_dialog_three:finalString[] fruits =newString[]{"苹果","雪梨","香蕉","葡萄","西瓜"};
                    alert =null;
                    builder =newAlertDialog.Builder(mContext);
                    alert = builder.setIcon(R.mipmap.ic_icon_fish).setTitle("选择你喜欢的水果,只能选一个哦~").setSingleChoiceItems(fruits,0,newDialogInterface.OnClickListener(){@Overridepublicvoid onClick(DialogInterface dialog,int which){Toast.makeText(getApplicationContext(),"你选择了"+ fruits[which],Toast.LENGTH_SHORT).show();}}).create();
                    alert.show();break;//多选列表对话框case R.id.btn_dialog_four:finalString[] menu =newString[]{"水煮豆腐","萝卜牛腩","酱油鸡","胡椒猪肚鸡"};//定义一个用来记录个列表项状态的boolean数组
                    checkItems =newboolean[]{false,false,false,false};
                    alert =null;
                    builder =newAlertDialog.Builder(mContext);
                    alert = builder.setIcon(R.mipmap.ic_icon_fish).setMultiChoiceItems(menu, checkItems,newDialogInterface.OnMultiChoiceClickListener(){@Overridepublicvoid onClick(DialogInterface dialog,int which,boolean isChecked){
                                    checkItems[which]= isChecked;}}).setPositiveButton("确定",newDialogInterface.OnClickListener(){@Overridepublicvoid onClick(DialogInterface dialog,int which){String result ="";for(int i =0; i < checkItems.length; i++){if(checkItems[i])
                                            result += menu[i]+" ";}Toast.makeText(getApplicationContext(),"客官你点了:"+ result,Toast.LENGTH_SHORT).show();}}).create();
                    alert.show();break;}}}
  • 相关阅读:
    JS模拟出 getElementsByClassName 功能
    如何为PDF文件添加书签
    Linux内核模块学习
    Linux字符设备驱动学习
    第53篇编译线程的初始化
    第51篇SharedRuntime::generate_native_wrapper()生成编译入口
    第50篇调用约定(2)
    第52篇即时编译器
    2021 阿里云容器服务年度盘点:企业级容器应用变化和技术趋势观察
    如何在零停机的情况下迁移 Kubernetes 集群
  • 原文地址:https://www.cnblogs.com/20193898liufa/p/14909589.html
Copyright © 2020-2023  润新知