如何使布局在Android中像AlertDialog一样运行

时间:2016-09-10 07:37:38

标签: android alertdialog

以下屏幕截图代表我的应用程序布局

LAYOUT WITH BUTTTONS

如果我点击屏幕上的任何地方(按代码按钮除外),是否有办法让完整的布局视图(父)完全消失。

2 个答案:

答案 0 :(得分:0)

要实现您所描述的内容,您需要在android中使用Dialog

    final Dialog dialog = new Dialog(context_of_ur_class);
    //if not title required
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //add your layout in this
    dialog.setContentView(R.layout.your_layout);
    dialog.setCanceledOnTouchOutside(true);
    dialog.show();

    //example for button 1
    Button one = (Button) dialog.findViewById(R.id.one);
    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //do what ever
        }
    });

答案 1 :(得分:0)

首先声明全局private AlertDialog mAlertDialog;

private void buttonsDialog() {
        AlertDialog.Builder mAlertBuilder = new AlertDialog.Builder(getActivity());
        mAlertBuilder.setCancelable(true);//you can change this
        mAlertDialog = mAlertBuilder.create();
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogLayout = inflater.inflate(R.layout.inform_dialog, null);
        mAlertDialog.setView(dialogLayout);

        Button button1 = (Button) dialogLayout.findViewById(R.id.alert_positive);
        Button button2 = (Button) dialogLayout.findViewById(R.id.alert_negative);

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //do your stuff
            }
        })
        mAlertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

buttonsDialog()中致电onCreate。并mAlertDialog.show显示对话框。