单击按钮事件时如何在对话框中设置图标(图像)

时间:2011-09-10 10:09:17

标签: java android button

我创建了一个按钮,当我单击该按钮时,显示一个新的对话框。在该对话框中我已设置图标并且text.text已显示但图标(图像)未显示在对话框中。 在这里我的编码

Button btnteam_westernbulldogs=new Button(this);
    btnteam_westernbulldogs.setId(team_westernbulldogsid);
    btnteam_westernbulldogs.setBackgroundResource(R.drawable.team_westernbulldogs);
          public void onClick(View v){  

              createbtnteam_westernbulldogs();
          }
  });


public void createbtnteam_westernbulldogs()
{
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setIcon(R.drawable.team_westernbulldogs);
    alertDialog.setMessage("What kind of Banner do you Want to Create?");
    alertDialog.setButton("Text", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
           createText();

       }
    alertDialog.setIcon(R.drawable.icon);
    alertDialog.show();
        }     });

3 个答案:

答案 0 :(得分:2)

   Try this..  

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setMessage("Are you sure you want to exit?") 
    .setIcon(R.drawable.icon)       
    .setCancelable(false)       
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
    {           
     public void onClick(DialogInterface dialog, int id) 
      {                
         MyActivity.this.finish();           
   }       
       })       
       .setNegativeButton("No", new DialogInterface.OnClickListener() 
        {           
           public void onClick(DialogInterface dialog, int id) 
            {                
             dialog.cancel();           
            }       
          });
        AlertDialog alert = builder.create();

答案 1 :(得分:1)

public void createbtnteam_westernbulldogs()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.team_westernbulldogs);
alertDialog.setTitle("What kind of Banner do you Want to Create?");
alertDialog.setButton("Text", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
       createText();

   }
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
}     });

答案 2 :(得分:0)

您必须使用非空字符串调用 setTitle ! 仅当还有标题时才会显示图标。