一个对话框,当单击确定按钮时,它应返回0,如果单击取消按钮,则应返回1

时间:2014-05-09 17:10:25

标签: android

public class DisplayAlert  implements OnClickListener
{
     int   j,t;


    @Override
    public void  onClick(View arg0) 
    {


        // TODO Auto-generated method stub

    }
    public void funval(int a)
    {
        j=a;

    }
    public int dis1(Context alertCall, String phoneNumber) {

        final ReturnValue ob=new ReturnValue();
        // TODO Auto-generated method stub
        new AlertDialog.Builder(alertCall).setTitle("SEND MESSAGE")
           .setMessage("Are you sure you want to send this msg to no ?   "+ phoneNumber)
 .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
     public void  onClick(DialogInterface dialog, int which) 
         { 
                     j=0;
                     ob.funval(j);


           }
         })
     .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int which) { 

               j=1;
              ob.funval(j);

        }
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
        return j;           
    }


}

**我已创建此类以显示一个对话框,当单击确定按钮时它应返回0并且如果单击取消按钮则应返回1.但是当我从我的项目中调用此函数时它不会等待单击确定或取消按钮,但返回初始值j **

final ReturnValue ob1=new ReturnValue();
     DisplayAlert ob = new DisplayAlert();      
     j= ob.dis1(c, phoneNumber);

1 个答案:

答案 0 :(得分:0)

final ResturnVale ob=new ReturnValue()之后的对话框代码放在自己的方法中。另外,添加create()行。然后拨打myDialog()。别忘了@Override onClick&#39>。

public void Dialog myDialog() {
   AlertDialog.Builder builder = new AlertDialog.Builder(alertCall);
       builder.setTitle("SEND MESSAGE")
       .setMessage("Are you sure you want to send this msg to no ?   "+ phoneNumber)
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
           @Override
           public void  onClick(DialogInterface dialog, int which) { 
                 j=0;
                 ob.funval(j);
           }
      })
      .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) { 
               j=1;
               ob.funval(j);

           }
      })
      .setIcon(android.R.drawable.ic_dialog_alert)
      .show();

      AlertDialog dialog = builder.create();
      return dialog;
}