简体   繁体   中英

AlertDialog & SnackBar alerts

Would it be possible to close an AlertDialog without having to click on/set a PositiveButton/NegativeButton?

        Delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            db.DeleteRecord(_id);
            db.close();
            Snackbar.make(textEntryView, "Removed", Snackbar.LENGTH_LONG).setDuration(700).show();
        }
    });

Using a custom button added to my layout I want to click on it and have it close the AlertDialog aswell.

I have a total of 3 buttons currently

  1. Update Record
  2. Delete Record
  3. Done (This one is using alert.setPositiveButton)

The other two I added in my layout so I could use Snackbar alert and I know I can do this by using setNeutralButton but it won't show a SnackBar alert.

I this code will work-

       final AlertDialog.Builder builder = new AlertDialog.Builder(this);
       final FrameLayout customView= (FrameLayout) View.inflate(this, R.layout.custome_view, null);
       final Button button = (Button) customView.findViewById(R.id.my_button);
       button.setText(mTvEmail.getText().toString());
       builder.setView(customView);
       final AlertDialog alertDialog = builder.create();
       alertDialog.show();

       button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              alertDialog.dismiss();  
            }
        });

To make it easier this is the whole thing

private void CreatePopup(Long id) {

    final LayoutInflater Manual = LayoutInflater.from(this);
    final View textEntryView = Manual.inflate(update, null);
    final EditText infoData = (EditText) textEntryView.findViewById(R.id.InfoData);
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final TextView TextSet = (TextView) textEntryView.findViewById(R.id.product);
    final Button Accept = (Button) textEntryView.findViewById(R.id.button);
    final Button Delete = (Button) textEntryView.findViewById(R.id.delete);

    final SQLite db = new SQLite(this);
    final Long _id = id;

    final SQLiteDatabase X = db.getReadableDatabase();
    final Cursor c;
    c = X.rawQuery("SELECT Product FROM Inventory WHERE _id =" + _id, null);
    c.moveToFirst();
    final String Data = c.getString(c.getColumnIndexOrThrow("Product"));
    TextSet.setText(Data);
    c.close();

    Accept.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (infoData.length() != 0) {
            final Editable Data = infoData.getText();
            db.UpdateRecord(Data, _id);
            db.close();
            Snackbar.make(textEntryView, "Updated", Snackbar.LENGTH_LONG).setDuration(700).show();

        }
        else {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "Input Quantity!", Toast.LENGTH_SHORT);
                toast.show();
            }
    }});
    Delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            db.DeleteRecord(_id);
            db.close();
            Snackbar.make(textEntryView, "Removed", Snackbar.LENGTH_LONG).setDuration(700).show();
        }
    });
    alert.setTitle("Update Quantity").setView(textEntryView);
    alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            CreateListView();
        }
    });
    alert.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM