Android自定义对话框

在代码中需要这项写就可以了:

//布局文件转换为view对象

         LayoutInflater inflaterDl = LayoutInflater.from(this);

         RelativeLayout layout = (RelativeLayout)inflaterDl.inflate(R.layout.layout_dialog, null );

        

         //对话框

         final Dialog dialog = newAlertDialog.Builder(SettingActivity.this).create();

         dialog.show();

         dialog.getWindow().setContentView(layout);

        

        

         //取消按钮

         Button btnCancel = (Button) layout.findViewById(R.id.dialog_cancel);

         btnCancel.setOnClickListener(new OnClickListener() {

          

           @Override

           public void onClick(View v) {

              Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show();           

           }

         });

        

        

         //确定按钮

         Button btnOK = (Button) layout.findViewById(R.id.dialog_ok);

         btnOK.setOnClickListener(new OnClickListener() {

          

           @Override

           public void onClick(View v) {

              Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show();           

           }

         });

        

        

         //关闭按钮

         ImageButton btnClose = (ImageButton) layout.findViewById(R.id.dialog_close);

         btnClose.setOnClickListener(new OnClickListener() {

          

           @Override

           public void onClick(View v) {

              dialog.dismiss();          

           }

         });

 

 

非常easy!自己动手吧。

你可能感兴趣的:(android)