2

我有这段代码,我想在其中以编程方式单击在当前 AlertDialog 之前显示的 AlertDialog 的正按钮。

下面是代码:

          ad.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                int[] serial=new int[10];
                int length = 0;
                int itemNotFound = 0;
                String firstItem=null;
                float firstPrate=0;
                float firstQuant=0;
                boolean status=true;
                for(int i=0;i<10;i++){
                serial[i]=rowValidator(i);
                if(serial[i]==0)
                    length=i;
                }


                for(int i=0;i<length;i++){
                    if(serial[i]==3)
                        itemNotFound++;
                }

                if(itemNotFound>0){

                    for(int i=0;i<length;i++){

                        if(serial[i]==3){
                            firstItem=items[i].getText().toString();
                            firstPrate=rate[i];
                            firstQuant=quant[i];
                            break;
                        }
                    }
                    AlertDialog d=new AlertDialog.Builder(PurchaseVoucher.this).create();
                    LayoutInflater inflater = (LayoutInflater)PurchaseVoucher.this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
                    View view=inflater.inflate(R.layout.alert_dialog, null);
                    d.setView(view);
                    d.setTitle("Add item(s) to inventory");
                    TextView tv=(TextView)view.findViewById(R.id.alertDialogID);
                    CheckBox cb=(CheckBox)view.findViewById(R.id.checkBox1);
                    cb.setVisibility(View.INVISIBLE);
                    tv.setText("Do you want to add "+firstItem+" to your inventory?");
                    if((itemNotFound-1)>0){
                        cb.setVisibility(View.VISIBLE);
                        cb.setText("Do this for the next "+(itemNotFound-1)+" conflict(s)");
                    }
                    d.show();

                    if(!cb.isChecked()){

                        PurchaseVoucher.this.dbControl.addOrUpdateStock(firstItem, firstPrate, 0, firstQuant);


                    } 

我想启动点击 if(!cb.isChecked) 不幸的是我可以为独立按钮设置按钮点击,但我将如何做到这一点?我希望点击 AlertDialog 广告的 positiveButton,以便显示一个新的 alertDialog d。

4

1 回答 1

4

使用此方法以编程方式单击

ad.getButton(AlertDialog.BUTTON_POSITIVE).performClick();
于 2013-09-07T08:11:11.430 回答