如何在按钮中调用MyDialogFragment

主要活动

public class MainActivity extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button bot = (Button) findViewById(R.id.button1);
        bot.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // ***HERE***
            }
        });
}


MyDialogFragment:

public static class MyDialogFragment extends DialogFragment {

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog

        // Pass null as the parent view because its going in the dialog layout
        builder.setView(inflater.inflate(R.layout.sign_in, null))

        // Add action buttons
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int id) {
                // sign in the user ...
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MyDialogFragment.this.getDialog().cancel();
            }
        });

        return builder.create();
    }
}

最佳答案

我想您只想要:
new MyDialogFragment().show(getFragmentManager(), "tag");

关于java - 如何在按钮Android中调用静态类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22787881/

10-09 13:01