AlertDialog在真实设备上但在仿真器上无法正常工作

时间:2017-10-02 18:47:25

标签: android alertdialog android-alertdialog

我的AlertDialog未在真实设备中显示setpositive按钮setnegative按钮,但在模拟器中显示。当我在真实设备上运行应用程序时,它会在真实设备中显示警告对话框,但不显示 yes no 。该应用程序在模拟器上运行良好。它在真实设备上也能很好地工作,但按钮不可见。

private void showAddFoodDialog() {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(FoodList.this);
    alertDialog.setTitle("Add new Food");
    alertDialog.setMessage("please fill full information");

    LayoutInflater inflater=this.getLayoutInflater();
    View add_menu_layout= inflater.inflate(R.layout.add_new_food_layout,null);

    edtName = (MaterialEditText) add_menu_layout.findViewById(R.id.edtName);
    edtDescription = (MaterialEditText) add_menu_layout.findViewById(R.id.edtDescription);
    edtPrice = (MaterialEditText) add_menu_layout.findViewById(R.id.edtPrice);
    edtDiscount = (MaterialEditText) add_menu_layout.findViewById(R.id.edtDiscount);
    btnSelect = (FButton) add_menu_layout.findViewById(R.id.btnSelect);
    btnUpload = (FButton) add_menu_layout.findViewById(R.id.btnUpload);

    //event for button
    btnSelect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            chooseImage(); //let user select image from gallery and save uri of  this image
        }
    });

    btnUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            uploadImage();
        }
    });
    alertDialog.setView(add_menu_layout);
    alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);

    //set button
    alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            //here ,just create new category
            if (newFood !=null)
            {
                foodList.push().setValue(newFood);
                Snackbar.make(rootLayout,"New category "+newFood.getName()+" was added",Snackbar.LENGTH_SHORT).show();
            }
        }
    });
    alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    alertDialog.show();
}

2 个答案:

答案 0 :(得分:0)

在显示之前创建alertDialog。

AlertDialog dialog = alertDialog.create();
dialog.show();

答案 1 :(得分:0)

这是在android

中创建适当对话框的示例
  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
  alertDialogBuilder.setMessage("Are you sure,
     You wanted to make decision");
  alertDialogBuilder.setPositiveButton("yes", 
     new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface arg0, int arg1) {
        Toast.makeText(MainActivity.this,"You clicked yes 
           button",Toast.LENGTH_LONG).show();
     }
  });

  alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
     Override
     public void onClick(DialogInterface dialog, int which) {
        finish();
     }
  });

  AlertDialog alertDialog = alertDialogBuilder.create();
  alertDialog.show();
}