我正在android studio中创建一个警告框

时间:2016-05-27 07:40:16

标签: java android xml

我正在尝试在android studio中构建一个警告框,我有2个xml和1个活动类,点击按钮我想要显示其他布局。 我收到一个错误,这是我的MainActivity代码

 cancelBookingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LayoutInflater inflater = getLayoutInflater();
            View alertLayout = inflater.inflate(R.layout.alert_cancel_confirm, null);
            final TextView disAgree = (TextView) alertLayout.findViewById(R.id.TextDisagree);
            final TextView Agree = (TextView) alertLayout.findViewById(R.id.TextAgree);


            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Current Booking");
            // This will set the view from XML inside ALertDialog
            alert.setView(alertLayout);
            // disabling cancel of AlertDialog on click of back button and outside touch

            alert.setCancelable(false);

            AlertDialog dialog = alert.create();
            dialog.show();
        }
    });

在这一行我收到错误

AlertDialog.Builder alert = new AlertDialog.Builder(this);
  

在Builder中无法应用   知道为什么我得到这个。

1 个答案:

答案 0 :(得分:0)

 AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);
            alert.setTitle("Current Booking");
            // This will set the view from XML inside ALertDialog
            alert.setView(alertLayout);
            // disabling cancel of AlertDialog on click of back button and outside touch

            alert.setCancelable(false);

            AlertDialog dialog = alert.create();
            dialog.show();