2012-03-19 15 views
27

वर्तमान में जब उपयोगकर्ता मेरा ऐप खोलता है तो AlertDialog उनसे पूछता है कि क्या वे प्रो संस्करण में अपग्रेड करना चाहते हैं। मुझे CheckBox को AlertDialog में जोड़ने की आवश्यकता है जो उपयोगकर्ता को ऐप खोलने पर ऐप अब AlertDialog दिखाएगा।चेतावनी संवाद में चेक बॉक्स को कैसे जोड़ें

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(" MY_TEXT"); 
    builder.setMessage(" MY_TEXT ") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Uri uri = Uri.parse("market://details?id=MY_APP_PACKAGE"); 
        Intent intent = new Intent (Intent.ACTION_VIEW, uri); 
        startActivity(intent);       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }).show(); 

किसी ने मुझे दिखा सकता है, तो कैसे AlertDialog कि ऐप अब AlertDialog दिखाने कर देगा करने के लिए एक CheckBox जोड़ने के लिए जब उपयोगकर्ता ऐप्लिकेशन खुलने:

यहाँ मैं अब AlertDialog के लिए है , वह महान होगा। अग्रिम धन्यवाद!

उत्तर

71

आपको AlertDialog.Builder ऑब्जेक्ट पर setView(View) विधि का उपयोग करना होगा। यह संदेश क्षेत्र और बटन के बीच View में पारित किया जाएगा। बस एक CheckBox के साथ एक View बढ़ और में पारित है कि यहाँ एक उदाहरण है:।

checkbox.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <CheckBox 
     android:id="@+id/checkbox" 
     style="?android:attr/textAppearanceMedium" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" /> 

</FrameLayout> 
अपनी गतिविधि में

कोड

View checkBoxView = View.inflate(this, R.layout.checkbox, null); 
CheckBox checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox); 
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

     // Save to shared preferences 
    } 
}); 
checkBox.setText("Text to the right of the check box."); 

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setTitle(" MY_TEXT"); 
    builder.setMessage(" MY_TEXT ") 
      .setView(checkBoxView) 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        Uri uri = Uri.parse("market://details?id=MY_APP_PACKAGE"); 
        Intent intent = new Intent (Intent.ACTION_VIEW, uri); 
        startActivity(intent);       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }).show(); 
+2

मुझे लगता है कि आपको findViewById द्वारा चेकबॉक्स में लौटाए गए दृश्य को डालने की आवश्यकता है। महान जवाब अन्यथा। – Gallal

+0

@ गॉलल आप सही हैं। मैंने उस सुधार को बनाया। –

+1

नमूना xml layut के साथ लंबे पाठ का उपयोग करने से बचने का प्रयास करें, यदि आपका टेक्स्ट बहुत लंबा है तो चेकबॉक्स छुपाया जाएगा – xDragonZ

1

सबसे पहले आपको उस लेआउट को परिभाषित करने की आवश्यकता है जिसमें संदेश और चेकबॉक्स शामिल है, जो बाद के विचारों पर अलर्ट अक्षम करने के लिए है। तो बजाय builder.setMessage बुलाने की, तो आप फोन करता हूँ:

builder.setView(myAlertViewWithDisablingCheckbox); 

फिर जब उपयोगकर्ता चेतावनी संवाद बटन पर क्लिक करता है, तो आप देखने के लिए कि कि चेकबॉक्स चेक किया गया है की जाँच करना होगा, और में है कि प्राथमिकताओं को सहेजने के अपने ऐप का SharedPreferences। फिर आप यह वरीयता का उपयोग यह निर्धारित करने के लिए कर सकते हैं कि यह चेतावनी संवाद उपयोगकर्ता को फिर से दिखाया जाना चाहिए या नहीं।

0

आप केवल एक आइटम के साथ एक multichoicelist इस्तेमाल कर सकते हैं :

final boolean checked = new boolean {false}; 
builder.setMultiChoiceItems(new String[]{"Remember decision}, checked, new DialogInterface.OnMultiChoiceClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i, boolean b) { 
        checked[i] = b; 
       } 
      }); 

फिरमें एक चेतावनी संवाद बटन केआप checked[0] का मूल्य की जाँच करें और में है कि मूल्य को बचा सकता है यदि आपके ऐप की Sharedpreferences:

builder.setNegativeButton("No", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) { 
         if(checked[0]){ 
          SharedPreferences.Editor editor = settings.edit(); 
          editor.putBoolean("preferences_never_buy_pro", true); 
          editor.apply(); 
         } 
         dialog.cancel(); 

        } 
       }); 
इस वरीयता आप तय कर सकते हैं संवाद भविष्य में फिर से दिखाया जाना चाहिए के साथ

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