如何在android中保持Dialog可见?

时间:2012-05-14 15:06:43

标签: android dialog progressdialog progress

我想在点击按钮后不要在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;

    }

1 个答案:

答案 0 :(得分: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();
            }
        }

    }