Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

android - Adding positive / negative Button to DialogFragment's Dialog

I've already written a DialogFragment. Now I've realized that I want it to have a positive and a negative button just like an AlertDialog. How can I achieve such a thing while maintaining the code that I've written?

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is how I figured it out. I erased the onCreateView and altered the onCreateDialog. This link actually had the answer so all the credit should go there. I've just posted it just in case anyone bumps in this question first.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...