如何在android片段中添加一个简单的OK信息对话框

时间:2014-11-30 14:43:16

标签: android android-fragments interface dialog

有人可以帮助我如何在我的片段类中添加OK信息对话框吗?我试过这个,但它不起作用,它在活动类中工作,但不在片段中。我一定是在遗漏某些东西,或者做错完全是错的,有人可以帮帮我吗?

编辑:一种方法,类似于;

public void rideInfor(ImageButton button, findViewById buttonID, String title, String message){


     ImageButton button = (ImageButton)rootView.findViewById(R.id.buttonID);
     button.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
                    builder1.setTitle(title);
                    builder1.setMessage(message);
                    // builder1.setIcon(R.drawable.ic_launcher);
                    builder1.setNeutralButton("Yes", new DialogInterface.OnClickListener()
                    {

                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            dialog.cancel();
                        }
                    });

                    builder1.show();

                }
            }); 

1 个答案:

答案 0 :(得分:0)

请尝试以下代码并告知我们:

编辑代码:

           });  
                    alertDailog.show();
    }
        });

方法:

         private void showAlertDialog(String msg, String title){
           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getActivity(), AlertDialog.THEME_HOLO_LIGHT);
            alertDialogBuilder.setTitle(title);
            alertDialogBuilder
                    .setMessage(msg);

    // Set the icon at the top left you want
    alertDialogBuilder.setIcon(R.drawable.icon);

     // true if cancellable want to true
            alertDialogBuilder.setCancelable(false);
            AlertDialog alertDailog = alertDialogBuilder.create();
    alertDialogBuilder.setNeutralButton("Yes", new DialogInterface.OnClickListener()
    {
                @Override
                public void onClick(View v) {
            // Ok button
            });


                alertDailog.show();
     }

完整代码:

ImageButton camBt = (ImageButton)view.findViewById(R.id.button1);
camBt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        getActivity(), AlertDialog.THEME_HOLO_LIGHT);
                alertDialogBuilder.setTitle("Title");
                alertDialogBuilder
                        .setMessage("Your Message");

        // Set the icon at the top left you want
        alertDialogBuilder.setIcon(R.drawable.icon);

         // true if cancellable want to true
                alertDialogBuilder.setCancelable(false);
                AlertDialog alertDailog = alertDialogBuilder.create();
        alertDialogBuilder.setNeutralButton("Yes", new DialogInterface.OnClickListener()
        {
                    @Override
                    public void onClick(View v) {
                // Ok button
                });


                    alertDailog.show();
            } 
        });// Close onCLickListener of Image Button