2013-09-03 10 views
35

पर सकारात्मक/नकारात्मक बटन जोड़ना हाय मैंने पहले से ही एक संवादप्रवाह लिखा है। अब मुझे एहसास हुआ है कि मैं चाहता हूं कि यह एक अलर्टडिअलॉग की तरह सकारात्मक और नकारात्मक बटन हो। मैंने जो कोड लिखा है, उसे बनाए रखने के दौरान मैं ऐसी चीज कैसे प्राप्त कर सकता हूं?डायलॉग फ्रैगमेंट के संवाद

public class DoublePlayerChooser extends DialogFragment { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);   

    setStyle(DialogFragment.STYLE_NORMAL,0); 



} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    return new AlertDialog.Builder(getActivity()) 
      .setTitle("title") 
      .setPositiveButton("OK", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         // do something... 
        } 
       } 
      ) 
      .setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         dialog.dismiss(); 
        } 
       } 
      ) 
      .create(); 
} 



@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.doubleplayerchooser, container, false); 
    getDialog().setTitle("Enter Players"); 

    firstPlayerPicker = (ImageButton) v.findViewById(R.id.imageButton1); 
    firstPlayerPicker.setOnClickListener(new OnClickListener() { 
     public void onClick(final View v){ 

      callContactPicker(1); 

     }  
    }); 

    secondPlayerPicker = (ImageButton) v.findViewById(R.id.ImageButton01); 
    secondPlayerPicker.setOnClickListener(new OnClickListener() { 
     public void onClick(final View v){ 

      callContactPicker(2); 

     }  
    }); 

    loadFromFile = (ImageButton) v.findViewById(R.id.imageButton2); 
    loadFromFile.setOnClickListener(new OnClickListener() { 
     public void onClick(final View v){ 



     }  
    }); 

    firstTextfield = (EditText) v.findViewById(R.id.editText1); 
    secondTextfield = (EditText) v.findViewById(R.id.EditText01); 

    firstImage = (ImageView) v.findViewById(R.id.imageView1); 
    secondImage = (ImageView) v.findViewById(R.id.ImageView01); 



    return v; 
} 

उत्तर

66

ठीक है, मैंने इसे कैसे समझ लिया। मैंने ऑनक्रेट व्यू को मिटा दिया और ऑनक्रेट डायलॉग को बदल दिया link वास्तव में इसका जवाब था इसलिए सभी क्रेडिट वहां जाना चाहिए। मैंने अभी इसे पोस्ट किया है यदि कोई भी इस धागे में पहले

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    AlertDialog.Builder b= new AlertDialog.Builder(getActivity()) 
    .setTitle("Enter Players") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       // do something... 
      } 
     } 
    ) 
    .setNegativeButton("Cancel", 
     new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       dialog.dismiss(); 
      } 
     } 
    ); 

    LayoutInflater i = getActivity().getLayoutInflater(); 

    View v = i.inflate(R.layout.doubleplayerchooser,null); 

    firstPlayerPicker = (ImageButton) v.findViewById(R.id.imageButton1); 
    firstPlayerPicker.setOnClickListener(new OnClickListener() { 
     public void onClick(final View v){ 

      callContactPicker(1); 

     }  
    }); 

    secondPlayerPicker = (ImageButton) v.findViewById(R.id.ImageButton01); 
    secondPlayerPicker.setOnClickListener(new OnClickListener() { 
     public void onClick(final View v){ 

      callContactPicker(2); 

     }  
    }); 

    loadFromFile = (ImageButton) v.findViewById(R.id.imageButton2); 
    loadFromFile.setOnClickListener(new OnClickListener() { 
     public void onClick(final View v){ 



     }  
    }); 

    firstTextfield = (EditText) v.findViewById(R.id.editText1); 
    secondTextfield = (EditText) v.findViewById(R.id.EditText01); 

    firstImage = (ImageView) v.findViewById(R.id.imageView1); 
    secondImage = (ImageView) v.findViewById(R.id.ImageView01); 


    b.setView(v); 
    return b.create(); 
} 
6

आप ओवरराइड करने के लिए DialogFragments onCreateDialog (...) विधि है:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    return new AlertDialog.Builder(getActivity()) 
      .setTitle("title") 
      .setPositiveButton("OK", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         // do something... 
        } 
       } 
      ) 
      .setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         dialog.dismiss(); 
        } 
       } 
      ) 
      .create(); 
} 

यहाँ से लिया: Android: disable DialogFragment OK/Cancel buttons

आप हो रही है त्रुटि संदेश ("अनुरोध के अनुसार सुविधा को बुलाया जाना चाहिए ... ") मैं अनुशंसा करता हूं:

अनुरोध से पहले setContentView() को कॉल न करें फ़ेचर() आपकी गतिविधि में या जहां भी आप इसे कॉल कर रहे हैं।

इसके अलावा

:

न कॉल setStyle (...) onCreate अंदर()।

इसे कॉल करें जहां आप अपना टुकड़ा बनाते हैं। DialogFragment here के बारे में

public class FireMissilesDialogFragment extends DialogFragment { 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setMessage(R.string.dialog_fire_missiles) 
      .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // FIRE ZE MISSILES! 
       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // User cancelled the dialog 
       } 
      }); 
    // Create the AlertDialog object and return it 
    return builder.create(); 
} 
} 

अधिक जानकारी:

YourDialogFragment f = new YourDialogFragment(Context); 
f.setStyle(...); 
// and so on ... 
+2

मैंने ऐसा किया है लेकिन मुझे सामग्री अपवाद जोड़ने से पहले एक अपवाद अनुरोध सुविधा कहा जाना चाहिए – Libathos

+0

फिर कृपया अपना कुछ कोड पोस्ट करें, और मेरा अद्यतन उत्तर देखें। –

+0

जब मैंने ऑनक्रेट व्यू को हटा दिया, तो यह त्रुटियों के बिना चलता है। अगर यह आपको – Libathos

1

स्पष्ट तरीका है।

// Your own onCreate_Dialog_View method 
public View onCreateDialogView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    return inflater.inflate(R.layout.your_layout, container); // inflate here 
} 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 

    // do something with 'view' 
} 

@NonNull 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    // setup dialog: buttons, title etc 
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()) 
      .setTitle(R.string.dialog_fragment_author_title) 
      .setNegativeButton(R.string.dialog_fragment_author_close, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          dialog.dismiss(); 
         } 
        } 
      ); 

    // call default fragment methods and set view for dialog 
    View view = onCreateDialogView(getActivity().getLayoutInflater(), null, null); 
    onViewCreated(view, null); 
    dialogBuilder.setView(view); 

    return dialogBuilder.create(); 
} 
0

इसमें कुछ समय पुरानी है लेकिन हाल ही में मैं onCreateView अधिभावी किया गया है जब AppCompatDialogFragment विस्तार। onCreateView में लौटने वाले उसी लेआउट में बस अपने स्वयं के बटन डालें - @style/Widget.AppCompat.Button.Borderless जैसी शैलियों का उपयोग करें।

जब आप एक एक्शन बटन क्लिक करते हैं तो डायलॉग स्वयं को खारिज करने के लिए अतिरिक्त बोनस मिलता है, खासकर जब इन कस्टम विचारों में कभी-कभी इनपुट की आवश्यकता होती है और जब आप बटन क्लिक करते हैं तो डायलॉग के ऑटो-क्लोज को अवरुद्ध करना चाहते हैं।

onCreateDialog में एक कस्टम दृश्य का उपयोग करना हमेशा गंदा महसूस होता है क्योंकि आप इसे बिना कंटेनर के फुला रहे हैं। Google ने एपीआई को नए v7 AlertDialog.Builder विधि setView(int layoutResId) के साथ थोड़ा सा अच्छा बनाने की कोशिश की, लेकिन आप findViewById पर कॉल नहीं कर सकते हैं।

आपको अपनी शैलियों में इस तरह की एक थीम जोड़नी चाहिए।xml:

<style name="AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <item name="colorPrimary">@color/material_light_blue_500</item> 
    <item name="colorPrimaryDark">@color/material_light_blue_800</item> 
    <item name="colorAccent">@color/material_light_blue_a400</item> 
    <item name="colorButtonNormal">@color/material_light_blue_500</item> 
    <item name="colorControlNormal">@color/material_light_blue_600</item> 
    <item name="colorControlActivated">@color/material_light_blue_a100</item> 
    <item name="colorControlHighlight">@color/material_light_blue_a100</item> 
</style> 

आप new AppCompatDialog(getActivity(), R.style.AlertDialog) वापस जाने के लिए और साथ ही अपने DialogFragment में onCreateDialog ओवरराइड चाहिए।

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