2017-04-16 64 views
0

是否可以關閉AlertDialog而無需點擊/設置PositiveButton/NegativeButton?AlertDialog&SnackBar提醒

 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警報。

+0

使用自定義佈局alertdialog的三個鍵... – rafsanahmad007

+1

是的,你可以打電話解僱()在你的自定義按鈕的onclick中,它會關閉對話框。 –

+0

對於您在問題中寫下的最後一行 - 您可以覆蓋中性按鈕的onClickListener並顯示快餐欄。 –

回答

0

我這個代碼將與工作

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

爲了更方便,這是整個事情

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