2012-07-01 18 views
21

नीचे दिए गए कोड में, मैं अलर्ट बॉक्स को कैसे खारिज कर सकता हूं? मैं एक स्मृति रिसाव का कारण नहीं बनना चाहता। मैं alertDialog पर .dismiss() कोशिश की, लेकिन बात नहीं बनी ... धन्यवाद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 
} 

उत्तर

39

के बारे में क्या खारिज (काम नहीं किया)?

आप या तो Dialog.dismiss(), या 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

क्या आप मुझे कोड में दिखा सकते हैं? मैं वास्तव में यहां उलझन में हूं, धन्यवाद – sneaky

+0

यकीन है, मेरा संपादन देखें। – FoamyGuy

+13

'AlertDialog.builder' में 'रद्द करें' विधि – xmen

1

alertDialog.setNeutralButton के बजाय का उपयोग करने के लिए सिर्फ alertDialog.setNegativeButton का उपयोग सक्षम होना चाहिए। dialog.cancel() का उपयोग करें, क्योंकि dialog.dismiss() अलर्ट संवाद के लिए एक विधि उपलब्ध नहीं है।

1

बस विधि बनाएं ओवरराइड करें और संवाद उदाहरण सहेजें। तो फिर तुम खारिज

@Override 
public AlertDialog create() { 
    this.dialog = super.create(); 
    return this.dialog; 
} 
कोड पर

कहीं कॉल कर सकते हैं:

dialog.dismiss(); 
10

आप इस तरह से उपयोग करना चाहिए अगर आप किसी भी बटन डाल करने के लिए नहीं करना चाहते हैं और कस्टम लेआउट है जिसमें आप टेक्स्टव्यू कहें और आप उस टेक्स्टव्यू पर क्लिक करके अलर्ट डायलॉग को खारिज करना चाहते हैं:

AlertDialog alertDialog = builder.show(); 

जाँच

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

यह भी सच है अगर आप कुछ हालत की जाँच के बाद अपनी गतिविधि में इसे कहीं और खारिज करने के लिए चाहते हैं।

+1

महान स्पष्टीकरण के साथ सही और सर्वोत्तम उत्तर –

28

निम्नलिखित कोड के साथ आप एक सूची बॉक्स अलर्टडिअलॉग दिखा सकते हैं और जब आप किसी आइटम पर दबाते हैं तो आप संवाद को खारिज करते हैं। कोड का क्रम आयातन है।

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

बिल्कुल सही! आपका बहुत बहुत धन्यवाद! – NightFury

+0

यही वह है जिसे मैं ढूंढ रहा था। धन्यवाद!!!! – Vicky

+0

धन्यवाद! यहां कुंजी कुंजीDialog.show() के साथ बनाए गए संवाद को खारिज करना है। निर्माता से एक संवाद बनाना और उस संवाद पर खारिज करने से हम उसी संवाद को खारिज नहीं करते हैं जब हम शो() कहते हैं। यह मुझे थोड़ा सा के लिए stumped था =) –

7

इस विधि कोड है कि आवश्यक है क़ौम .. नम्रता के पहले जवाब से

@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

कोड बहुत सरल है:

final AlertDialog show = alertDialog.show(); 
अंत में, उदाहरण के लिए बटन की क्रिया में

:

show.dismiss(); 

एक्सपैम्प के लिए एक कस्टम alertdialog साथ ले: जावा पर

enter image description here

कोड, आप एक वस्तु 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(); 
      } 
     }); 
    } 
} 

कोड एक्सएमएल layout_test।एक्सएमएल

<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> 

अंत में, गतिविधि पर कॉल:

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

महान समाधान। मुझे बहुत मदद की! –

0

मैं इस कोशिश की और काम किया!

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

धन्यवाद ............. – Varma

0

खारिज या रद्द AlertDialog.Builder

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

आप संवाद इंटरफेस पर dismiss() कॉल करनी होगी करने के लिए।

संबंधित मुद्दे