首页 > 代码库 > AlertDialog的基本使用

AlertDialog的基本使用

代码小示例:

  FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
                View viewDialog = inflater.inflate(R.layout.new_cost, null);
                builder.setTitle("new Cost");
                builder.setView(viewDialog);
                final EditText et_title = (EditText) viewDialog.findViewById(R.id.et_title);
                final EditText et_money = (EditText) viewDialog.findViewById(R.id.et_money);
                final DatePicker dp = (DatePicker) viewDialog.findViewById(R.id.date);
                //builder.setPositiveButton("确认", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                            String title = et_title.getText().toString();
                            String money = et_money.getText().toString();
                            String date = dp.getYear() + "-" + (dp.getMonth()+1) + "-" + dp.getDayOfMonth();

                            CostBean costBean = new CostBean(title,date,money);
                            helper.insert(costBean);
                            list.add(costBean);
                            adapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                final AlertDialog alertDialog = builder.create();
                alertDialog.show();
//                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        String title = et_title.getText().toString();
//                        String money = et_money.getText().toString();
//                        String date = dp.getYear() + "-" + (dp.getMonth()+1) + "-" + dp.getDayOfMonth();
//                        if (title.isEmpty()){
//                            Toast.makeText(MainActivity.this,"请输入消费条目",Toast.LENGTH_LONG).show();
//                            return;
//                        }
//                        if (money.isEmpty()){
//                            Toast.makeText(MainActivity.this,"请输入消费金额",Toast.LENGTH_LONG).show();
//                            return;
//                        }
//
//                        CostBean costBean = new CostBean(title,date,money);
//                        helper.insert(costBean);
//                        list.add(costBean);
//                        adapter.notifyDataSetChanged();
//                        alertDialog.dismiss();
//                    }
//                });

            }
        });

  如果点击确定时,需要根据弹窗中的控件状态来判断是否关闭弹窗,则需要使用注释状态下的代码。

 

AlertDialog的基本使用