材料对话框按钮单击事件行为不端

时间:2018-02-25 17:13:04

标签: android dialog material-design

您好我在我的应用中使用affolestads material dialogs,但最近我注意到了一些事情。积极的button点击事件有时表现得很好但有时它只是没有启动我的{{ 1}}第一次,所以我必须再次点击它。

查看我的代码,并可能会突出显示我的错误:

AsyncTask

我是通过@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_remove, menu); //menu hiding and invalidation this.invalidateOptionsMenu(); MenuItem item = menu.findItem(R.id.delete); MenuItem item1 = menu.findItem(R.id.pay); if (mFruitList.size() > 0) { item.setVisible(true); item1.setVisible(true); } if (mSpicesList.size() > 0) { item.setVisible(true); item1.setVisible(true); } if (mVeggyList.size() > 0) { item.setVisible(true); item1.setVisible(true); } if (mCerealsList.size() > 0) { item.setVisible(true); item1.setVisible(true); } return true; } //delete selected items @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.delete: //item removal for (int i = mFruitList.size() - 1; i >= 0; i--) { if (mFruitList.get(i).selected) { Product selectedProduct = mFruitList.get(i); FrtHelper.removeProduct(selectedProduct); mFruitList.remove(selectedProduct); } } adapterFruit.notifyDataSetChanged(); for (int i = mSpicesList.size() - 1; i >= 0; i--) { if (mSpicesList.get(i).selected) { Product selectedProduct = mSpicesList.get(i); SpcHelper.removeProduct(selectedProduct); mSpicesList.remove(selectedProduct); } } adapterSpices.notifyDataSetChanged(); for (int i = mVeggyList.size() - 1; i >= 0; i--) { if (mVeggyList.get(i).selected) { Product selectedProduct = mVeggyList.get(i); VgyHelper.removeProduct(selectedProduct); mVeggyList.remove(i); } } adapterVeggies.notifyDataSetChanged(); for (int i = mCerealsList.size() - 1; i >= 0; i--) { if (mCerealsList.get(i).selected) { Product selectedProduct = mCerealsList.get(i); CrlHelper.removeProduct(selectedProduct); mCerealsList.remove(i); } } adapterCereals.notifyDataSetChanged(); return true; case R.id.pay: //price calculation adapterFruit.notifyDataSetChanged(); adapterCereals.notifyDataSetChanged(); adapterVeggies.notifyDataSetChanged(); adapterSpices.notifyDataSetChanged(); int subTotal1 = 0; for (Product p : mFruitList) { int quantity = FrtHelper.getProductQuantity(p); subTotal1 += p.price * quantity; } int subTotal2 = 0; for (Product p : mSpicesList) { int quantity = SpcHelper.getProductQuantity(p); subTotal1 += p.price * quantity; } int subTotal3 = 0; for (Product p : mVeggyList) { int quantity = VgyHelper.getProductQuantity(p); subTotal1 += p.price * quantity; } int subTotal4 = 0; for (Product p : mCerealsList) { int quantity = CrlHelper.getProductQuantity(p); subTotal1 += p.price * quantity; } Total = subTotal1 + subTotal2 + subTotal3 + subTotal4; GrandTotal = (Total * 16 / 100) + Total; createPdf(); ShowDialog("Place order worth" + " " + String.valueOf(GrandTotal)); return true; case android.R.id.home: drawer.openDrawer(GravityCompat.START); return true; } return true; } private void takeScreenShot() { try { View u = findViewById(R.id.recycler_parent); NestedScrollView z = findViewById(R.id.cart_layout); int totalHeight = z.getChildAt(0).getHeight(); int totalWidth = z.getChildAt(0).getWidth(); bitmap = loadBitmapFromView(u, totalWidth, totalHeight); bitmap = Bitmap.createScaledBitmap(bitmap, totalWidth, totalHeight, true); Image image; String path = Environment.getExternalStorageDirectory() + "/e-Nunua/" + "Order" + ".pdf"; File filepath = new File(Environment.getExternalStorageDirectory() + "/e-Nunua/"); if (!filepath.exists()) { final boolean mkdirs = filepath.mkdirs(); } File myPath = new File(path); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); image = Image.getInstance(stream.toByteArray()); image.setAbsolutePosition(0, 0); Document document = new Document(image); PdfWriter.getInstance(document, new FileOutputStream(myPath)); document.open(); document.addAuthor("kamble"); document.add(image); document.close(); } catch (Exception i1) { i1.printStackTrace(); } } public void ShowDialog(String title) { new MaterialDialog.Builder(CrtActivity.this) .content("Note that:\n\n1.This amount is inclusive of 16% tax.\n\n2.By clicking yes you agree to our terms of sales and are bound by them.Any action contrary to the terms after cognition of the request shall leave you liable to any action placed by the app owner.The full copy of the terms of sale and use can be found in the app settings.Please read and understand them before placing any order.\n\nClick yes to place order or no to cancel.") .title(title) .icon(getResources().getDrawable(R.mipmap.launcher)) .positiveText("Yes") .negativeText("No!") .cancelable(false) .onPositive(new MaterialDialog.SingleButtonCallback() { @Override public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { if (CtgActivity.isInternetWorking()) { SendEmail sendEmail = new SendEmail(); sendEmail.execute(); } else { Toast.makeText(getBaseContext(), "Please connect to a working network and retry", Toast.LENGTH_LONG).show(); } } }).show(); } 点击来调用它,然后执行了两个menu item。在AsyncTasks启动时,另一个在dialog按钮的点击事件中

1 个答案:

答案 0 :(得分:1)

ShowDialog()不接收标题..制作标题全局变量...如果不行......将计算放在ShowDialog()内......可能会导致延迟。< / p>