2012-07-01 37 views
21

Trong đoạn mã dưới đây, làm cách nào để loại bỏ hộp cảnh báo? Tôi không muốn gây ra rò rỉ bộ nhớ. Tôi đã thử các .dismiss() trên alertDialog, nhưng điều đó không làm việc ... Cảm ơnLàm cách nào để loại bỏ AlertDialog.Builder?

// User pressed the stop button 
public void StopMsg_button_action(View view){ 
    final EditText password_input = new EditText(this); // create an text input field 
    password_input.setHint("Enter Password"); // put a hint in it 
    password_input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); // change it to password type 

    AlertDialog.Builder alertDialog = new Builder(this); // create an alert box 
    alertDialog.setTitle("Enter Password"); // set the title 
    alertDialog.setView(password_input); // insert the password text field in the alert box 
    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // define the 'OK' button 
     public void onClick(DialogInterface dialog, int which) { 
      String entered_password = password_input.getText().toString(); 
      if (entered_password.equals(my_password)) { 
       locManager.removeUpdates(locListener); // stop listening for GPS coordinates 
       startActivity(new Intent(Emergency_1Activity.this,Main_MenuActivity.class)); // go to main menu 
      } else { 
       alert("Incorrect Password"); 
      } 
     } 
    }); 
    alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }); 
    alertDialog.show(); // show the alert box 
} 

Trả lời

39

gì đã không làm việc về sa thải()?

Bạn sẽ có thể sử dụng một trong hai Dialog.dismiss(), hoặc Dialog.cancel()

alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() { // define the 'Cancel' button 
    public void onClick(DialogInterface dialog, int which) { 
     //Either of the following two lines should work. 
     dialog.cancel(); 
     //dialog.dismiss(); 
    } 
}); 
+0

bạn có thể chỉ cho tôi mã không? Tôi thực sự bối rối ở đây, cảm ơn – sneaky

+0

chắc chắn, hãy xem bản chỉnh sửa của tôi. – FoamyGuy

+13

'AlertDialog.Builder' không có phương thức' hủy' – xmen

1

Thay vì alertDialog.setNeutralButton chỉ cần sử dụng alertDialog.setNegativeButton. Sử dụng dialog.cancel(), vì dialog.dismiss() không phải là phương thức có sẵn cho hộp thoại Cảnh báo.

1

Chỉ cần ghi đè phương thức tạo và lưu phiên bản hộp thoại. Sau đó, bạn có thể gọi miễn nhiệm

@Override 
public AlertDialog create() { 
    this.dialog = super.create(); 
    return this.dialog; 
} 

Một nơi nào đó vào mã:

dialog.dismiss(); 
10

Bạn phải sử dụng theo cách này nếu bạn không muốn đặt bất kỳ nút và có bố trí tùy chỉnh mà bạn có nói textview và bạn muốn bỏ qua hộp thoại cảnh báo khi nhấp vào văn bản đó:

AlertDialog alertDialog = builder.show(); 

rồi kiểm tra

if(alertDialog != null && alertDialog.isShowing()){ 
     alertDialog.dismiss(); 
} 

Điều này cũng đúng nếu bạn muốn bỏ nó ở một nơi khác trong hoạt động của bạn sau khi kiểm tra một số điều kiện.

+1

câu trả lời hoàn hảo và tốt nhất với lời giải thích tuyệt vời –

28

Với mã sau bạn có thể hiển thị ListBox AlertDialog và khi bạn nhấn vào một mục bạn loại bỏ Hộp thoại. Thứ tự mã là importan.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); 

String names[] ={"A","B","C","D"}; 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,names); 

LayoutInflater inflater = getLayoutInflater(); 
View convertView = (View)inflater.inflate(R.layout.list_layout, null); 
ListView lv = (ListView) convertView.findViewById(R.id.listView1); 
lv.setAdapter(adapter); 
alertDialog.setView(convertView); 
alertDialog.setTitle("List"); 
final AlertDialog ad = alertDialog.show(); 

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{ 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    { 
     //Toast.makeText(mContext, position, Toast.LENGTH_LONG).show(); 
     ad.dismiss(); 
    } 
}); 
+0

Hoàn hảo! Cảm ơn rất nhiều! – NightFury

+0

Đó là những gì tôi đang tìm kiếm. Cảm ơn!!!! – Vicky

+0

Cảm ơn bạn! Chìa khóa ở đây là loại bỏ hộp thoại được tạo bằng alertDialog.show(). Tạo một hộp thoại từ trình tạo và gọi bỏ qua trên hộp thoại đó không loại bỏ cùng một hộp thoại mà chúng ta tạo ra khi chúng ta gọi show(). Điều này đã cho tôi stumped cho một chút =) –

7

Phương pháp này demo mã đó là cần thiết .. từ câu trả lời trước đó Namrata của

@SuppressLint("InflateParams") 
private static void showDialog(Activity activity, int layoutId) 
{ 
    LayoutInflater inflater = activity.getLayoutInflater(); 
    View dialoglayout = inflater.inflate(layoutId, null); 

    AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
    builder.setView(dialoglayout); 

    CustomDialog.doCustomLogic(activity, layoutId, dialoglayout); 

    final AlertDialog alertDialog = builder.show(); 

    // caller assumes there will be a btn_close element present 
    Button closeNowBtn = (Button) dialoglayout.findViewById(R.id.btn_close); 
    closeNowBtn.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      alertDialog.dismiss(); 
     } 
    }); 
} 
13

Mã này rất đơn giản:

final AlertDialog show = alertDialog.show(); 

cuối cùng trong hành động nút ví dụ:

show.dismiss(); 

Dành cho examp le với một alertdialog tùy chỉnh:

enter image description here

Mã trên java, bạn có thể tạo một Object AlertDialog:

public class ViewAlertRating { 

    Context context; 
    public ViewAlertRating(Context context) { 
     this.context = context; 
    } 

    public void showAlert(){ 

     AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     View alertView = inflater.inflate(R.layout.layout_test, null); 
     alertDialog.setView(alertView); 

     final AlertDialog show = alertDialog.show(); 

     Button alertButton = (Button) alertView.findViewById(R.id.btn_test); 
     alertButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       show.dismiss(); 
      } 
     }); 
    } 
} 

Mã XML layout_test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 


    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Valoración" 
     android:id="@+id/text_test1" 
     android:textSize="20sp" 
     android:textColor="#ffffffff" 
     android:layout_centerHorizontal="true" 
     android:gravity="center_horizontal" 
     android:textStyle="bold" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:background="#ff37dabb" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" /> 


    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:layout_marginTop="15dp"> 

     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:id="@+id/edit_test" 
      android:hint="Descripción" 
      android:textColor="#aa000000" 
      android:paddingLeft="10dp" 
      android:paddingRight="10dp" 
      android:textColorHint="#aa72777a" 
      android:gravity="top" /> 
    </LinearLayout> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center_horizontal" 
     android:paddingTop="10dp" 
     android:paddingLeft="15dp" 
     android:paddingRight="15dp" 
     android:paddingBottom="15dp" > 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <LinearLayout 
       android:orientation="horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:weightSum="1.00" 
       android:gravity="right" > 

       <Button 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Enviar" 
        android:id="@+id/btn_test" 
        android:gravity="center_vertical|center_horizontal" 
        android:textColor="#ffffffff" 
        android:background="@drawable/btn_flat_blue_selector" /> 
      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

cuối cùng, kêu gọi Hoạt động:

ViewAlertRating alertRating = new ViewAlertRating(this); 
alertRating.showAlert(); 
+1

giải pháp tuyệt vời. Giúp tôi rất nhiều! –

0

tôi đã cố gắng này và làm việc!

.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
       alertDialog.setCancelable(true); 
      } 
     }); 
+0

Cảm ơn bạn ............. – Varma

0

Để bỏ hoặc hủy AlertDialog.Builder

dialog.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialogInterface, int i) { 
     dialogInterface.dismiss() 
    } 
}); 

Bạn phải gọi dismiss() trên giao diện hộp thoại.

Các vấn đề liên quan