我有这个代码,我想以编程方式单击当前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。
答案 0 :(得分:3)
使用此方法以编程方式单击
ad.getButton(AlertDialog.BUTTON_POSITIVE).performClick();