簡體   English   中英

AlertDialog和SnackBar警報

[英]AlertDialog & SnackBar alerts

是否可以關閉AlertDialog而不需要單擊/設置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();
        }
    });

使用添加到我的布局中的自定義按鈕,我想單擊它並使它也關閉AlertDialog。

我目前共有3個按鈕

  1. 更新記錄
  2. 刪除記錄
  3. 完成(這是使用alert.setPositiveButton的)

我在布局中添加了另外兩個,因此我可以使用Snackbar警報,並且我知道可以通過使用setNeutralButton來做到這一點,但不會顯示SnackBar警報。

我這段代碼可以工作-

       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();  
            }
        });

為了更容易,這就是全部

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM