0

我不想在单击按钮后隐藏android中的进度对话框,我只想在保持Dialog出现的同时执行一些操作,我有以下用于创建对话框的代码:

public Dialog onCreateDialog(int id){
        switch(id){
        case 0 :
            // Alert Dialog
            return null;
        case 1:

            progressDialog = new ProgressDialog(this);
            progressDialog.setIcon(R.drawable.ic_launcher);
            progressDialog.setTitle("Play audio file");

            progressDialog.setCancelable(false);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    /*      progressDialog.setButton(DialogInterface.BUTTON_POSITIVE,"Play",
                    new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    Toast.makeText(getBaseContext(),"Hide clicked!",Toast.LENGTH_SHORT).show();
                }
            });*/

            progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Close", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(),"Cancel clicked!",Toast.LENGTH_SHORT).show();

                }
            });
            progressDialog.setButton("Play", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getBaseContext(),"Cancel clicked!",Toast.LENGTH_SHORT).show();

                }
            });
            return progressDialog;
        }
        return null;

    }
4

1 回答 1

0

我使用了一个带有自定义布局的 AlertDialog。通过控制自定义布局中的 onClickListeners,我可以决定何时让对话框消失。我对对话框不太了解,但这对我有用,也应该对你有用,虽然我不知道你是否可以专门使用 ProgressDialog

    @Override
    public void openDialog(Context context) {

        // declare the dialog object
        AlertDialog.Builder builder;
        AlertDialog alertDialog = null;

        // inflate the layout to use inside it
        View layout = ((Activity)context).getLayoutInflater().inflate(R.layout.list_selector_horizontal,null, false);

        TextView text = (TextView) layout.findViewById(R.id.list_selector_textView1);
        text.setText("Select a product:");

        // set the dialog to use the layout
        builder = new AlertDialog.Builder(context);
        builder.setView(layout);                        
        alertDialog = builder.create(); 

        // *important* Set this to false to stop users dismissing the dialog when they tap outside of it
        alertDialog.setCanceledOnTouchOutside(false);

        // populate the layout with some more views
        LinearLayout scrollContainer = (LinearLayout) layout.findViewById(R.id.list_selector_linearLayout2);                
        for(int i = 0;i<products.size();i++){
            Product product = products.get(i);
            View view = product.getView(context);

            // set the onClickListener for the product view
            view.setOnClickListener(new ProductListOnClickListener(alertDialog, product, this, v));
            view.setPadding(10, 10, 10, 10);
            scrollContainer.addView(view);
        }

        // show the dialog
        alertDialog.show();

    }

然后在 onClickListener 中:

class ProductListOnClickListener implements OnClickListener{

        private AlertDialog alertDialog;                    
        private Product p;
        private View view;
        private boolean dismiss = false;

        public ProductListOnClickListener(AlertDialog alertDialog, Product p, View view) {              
            this.alertDialog = alertDialog;
            this.p = p;
            this.view = view;
        }

        @Override
        public void onClick(View v) {
            // do whatever

            if(dismiss){
                // dismiss dialog
                alertDialog.dismiss();
            }
        }

    }
于 2012-05-14T19:00:59.193 回答