我在三个按钮(衣服,配件,鞋子)上使用相同的警报对话框。有没有办法 - 当点击button clothing
时,对话框会弹出title clothing
并分别弹出所有其他按钮。
默认情况下,代码alert dialog box title is button label
dialog.setTitle("Button label")
public void onClick(View v) {
if(v == clothing){
mDialog();
}
if(v == acessories) {
mDialog();
}
if(v == shoes){
mDialog();
}}
public mDialog(){
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("Button label");
dialog.setIcon(android.R.drawable.ic_dialog_info);
dialog.setMessage("click ok to close ");
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
答案 0 :(得分:2)
将Button标签传递给方法mDialog
...并在mDialog
setTitle
方法中传递string
。
mDialog(button.getText().toString);
在这样的方法中使用:
public mDialog(String name){
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setTitle(name);
答案 1 :(得分:1)
使用它:
public void onClick(View v) {
if(v == clothing){
mDialog("clothing");
}
if(v == acessories) {
mDialog("acessories");
}
if(v == shoes){
mDialog("shoes");
}}
将您想要的字符串传递给mDialog,并传递给mDialog
:
public mDialog(String title){
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setTitle(title);
dialog.setIcon(android.R.drawable.ic_dialog_info);
dialog.setMessage("click ok to close ");
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
答案 2 :(得分:0)