我有以下代码。它显示了一个带有文本视图和一些按钮的对话框。按下按钮时,对话框应该关闭。但它没有。它有问题吗?我尝试了 dialog.dismiss 和 dialog.cancel 但它不起作用。
有什么解决办法?
AlertDialog.Builder alert = new AlertDialog.Builder(
Gestionarez.this);
final TextView Dtv = new TextView(Gestionarez.this);
printeaza=new Button(Gestionarez.this);
stergere=new Button(Gestionarez.this);
trimitere=new Button(Gestionarez.this);
final AlertDialog dialog = alert.create();
trimitere.setText("Trimite");
trimitere.setTextSize(10);
trimitere.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
printeaza.setText("Printeaza");
printeaza.setTextSize(10);
printeaza.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
dialog.cancel();
}
});
stergere.setText("Sterge");
stergere.setTextSize(10);
stergere.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
boolean deleted = file.delete();
Toast.makeText(Gestionarez.this, str+ " a fost sters ",
Toast.LENGTH_SHORT).show();
lv2.invalidateViews();
}
});
LinearLayout ldialog = new LinearLayout(Gestionarez.this);
LinearLayout ldialogb = new LinearLayout(Gestionarez.this);
ldialog.setOrientation(LinearLayout.VERTICAL);
ldialogb.setOrientation(LinearLayout.HORIZONTAL);
Dtv.setText(text);
ldialog.addView(Dtv);
ldialogb.addView(trimitere);
ldialogb.addView(printeaza);
ldialogb.addView(stergere);
ldialog.addView(ldialogb);
ldialogb.setGravity(Gravity.CENTER | Gravity.BOTTOM);
Dtv.setGravity(Gravity.CENTER | Gravity.BOTTOM);
Dtv.setPadding(60, 60, 60, 60);
alert.setView(ldialog);
alert.show();
}
});
}