自定义对话框,其中包含指向网页的按钮

时间:2016-05-14 07:53:12

标签: android

我正在尝试创建一个弹出对话框,当您单击导航抽屉中的某个项目时,该对话框将会打开。我想在其中放置按钮,这将导致网页。由于我对Android很新,我不知道如何实现它。为了向您展示,我希望它看起来像这样:

Pop up Dialog

此致

3 个答案:

答案 0 :(得分:0)

您可以扩展提供自定义布局的DialogFragment,如下所示:

public static class MyDialogFragment extends DialogFragment {

    static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_dialog, container, false); 
        // R.layout.fragment_dialog is a .xml layout YOU provide and it is located into the /layout folder of the project!

        // Provide the behavior for the dialog here, say react to click on the buttons and so on...

        return v;
    }   
}

在托管对话框的活动中,您提供了一种显示它的方法:

void showDialog() {

    mStackLevel++;

    // DialogFragment.show() will take care of adding the fragment
    // in a transaction.  We also want to remove any currently showing
    // dialog, so make our own transaction and take care of that here.
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    Fragment prev = getFragmentManager().findFragmentByTag("dialog"); // Checking if there's already a dialog with the tag "dialog"
    if (prev != null) {
        ft.remove(prev);
    }

    ft.addToBackStack(null);

    // Create and show the dialog.
    DialogFragment newFragment = MyDialogFragment.newInstance();
    newFragment.show(ft, "dialog"); // Setting a tag "dialog" for the newly created dialog
}

答案 1 :(得分:0)

你可以这样做

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
            alertDialogBuilder.setTitle("you");
            alertDialogBuilder.setView(R.layout.custom_layout);


            alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User clicked OK
                }
            });
            alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
            final AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.findViewById(your button id).setOnClickListerner(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            });
            alertDialog.show();

如果你不想设置确定取消按钮,只需删除该代码,我希望它能正常工作。

答案 2 :(得分:0)

您可以创建 custom Dialog

。只需将xml视图扩展为Dialog

即可
public void customDialogBox(Context mContext){
        // Create custom dialog object
        final Dialog dialog = new Dialog(mContext);
        // Include dialog.xml file
        dialog.setContentView(R.layout.dialog);
        // Set dialog title
        dialog.setTitle("Custom Dialog");

        // set values for custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.textDialog);
        text.setText("Custom dialog Android example.");

        dialog.show();

        Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
        // if decline button is clicked, close the custom dialog
        declineButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Close dialog
                dialog.dismiss();
            }
        });


    }