0

我正在制作一个需要多次显示对话框的应用程序,我不想一次又一次地编写对话框的代码,那么我应该如何制作一个通用对话框以便我可以在选择是时执行不同的功能或在不同的地方没有。请帮忙,我尝试了很多但没有得到解决方案。

4

5 回答 5

2

您可以创建如下方法:

public Dialog showDialog(String title, String msg, final Activity activity) {

        final AlertDialog alertDialog = new AlertDialog.Builder(activity)
                .create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(msg);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();

                    activity.finish();

            }
        });
        alertDialog.show();

        return alertDialog;

    }

无论您想在哪里进行对话,只需调用showDialog("Title","your message",Acitivity);

于 2013-09-19T10:03:44.897 回答
0

我只是将我的现成代码粘贴到这里,就像我对我的项目所做的那样。请相应地修改不同的标签或向我寻求进一步的帮助!

  • 使用 android v4 支持库可以使以下对话框向后兼容 1.6
  • 对话框不会在设备旋转时自行关闭。

这就是您的对话框的样子: 在此处输入图像描述

第 1 步:使用名称创建一个新类,ConfirmationDialog并在其中添加以下代码。

public class ConfirmationDialog extends DialogFragment {

    private Handler mHandler;
private Bundle mBundle;
private String messageToShow;
private String dialogTitle;

public ConfirmationDialog(Handler mHandler, String dialogTitle,
        String messageToShow) {
    this.mHandler = mHandler;
    mBundle = new Bundle();
    this.messageToShow = messageToShow;
    this.dialogTitle = dialogTitle;
    Log.i(MyConstants.LOG_TAG,
            "------------CONSTRUCTOR EXECUTED------------");
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    Log.i(MyConstants.LOG_TAG, "------------ONCREATE EXECUTED------------");
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle(dialogTitle);
    builder.setIcon(R.drawable.ic_alerts_and_states_warning);
    builder.setMessage(messageToShow);
    builder.setPositiveButton(getString(R.string.ok),
            new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mBundle.clear();
                    mBundle.putInt(MyConstants.CONFIRM_NOTE_DELETE_TAG,
                            MyConstants.DIALOG_OK);
                    Message m = new Message();
                    m.setData(mBundle);
                    mHandler.sendMessage(m);
                }
            });

    builder.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            mBundle.clear();
            mBundle.putInt(MyConstants.CONFIRM_NOTE_DELETE_TAG,
                    MyConstants.DIALOG_CANCEL);
            Message m = new Message();
            m.setData(mBundle);
            mHandler.sendMessage(m);
        }
    });
    Dialog theDialog = builder.create();
    theDialog.setCanceledOnTouchOutside(true);
    return theDialog;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onDestroyView() {
    if (getDialog() != null && getRetainInstance()) {
        getDialog().setDismissMessage(null);
    }
    super.onDestroyView();
    }
}

然后在要显示对话框的片段中添加以下代码:

Handler mHandler = new Handler() {
    public void handleMessage(Message m) {
        Bundle mBundle = new Bundle();
        mBundle = m.getData();
        Log.i(MyConstants.LOG_TAG, "Got the data returned by the Dialog!");

        int dialogOkCancel = MyConstants.DIALOG_CANCEL;
        if (!mBundle.isEmpty())
            dialogOkCancel = mBundle.getInt(
                    MyConstants.CONFIRM_NOTE_DELETE_TAG,
                    MyConstants.DIALOG_CANCEL);
        if (dialogOkCancel == MyConstants.DIALOG_OK) {
            Log.i(MyConstants.LOG_TAG, "Ok selected");              
        } else if (dialogOkCancel == MyConstants.DIALOG_CANCEL) {
            Log.i(MyConstants.LOG_TAG, "Cancel selected");
        }
    } // end handleMessage
};

第 3 步:然后在要显示对话框时执行此代码:

ConfirmationDialog mConfirmationDialog = new ConfirmationDialog(
                        mHandler, "Delete Routine Task",
                        "Are you sure to delete Routine Task?");
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager
                        .beginTransaction();
                fragmentTransaction.add(mConfirmationDialog,
                        MyConstants.CONFIRM_NOTE_DELETE_TAG);
                fragmentTransaction.commit();
于 2013-09-19T10:10:46.823 回答
0

将对话框声明为公共

        public Dialog dialog_box;

初始化它......在oncreate......

    dialog_box = new Dialog(HSMPDFViewer.this,android.R.style.Theme_Translucent);
    dialog_box.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog_box.setCancelable(true);

为它创建一个函数......并使用它......

     /**
 * Alert Dialog 
 */
public void AlertDialogMessage() {
    dialog_text.setText("Problem in loading ..");
    dialog_btnOk.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            dialog_box.dismiss();
        }
    });
    dialog_box.show();

}
于 2013-09-19T10:01:43.200 回答
0

您可以尝试从 Dialog/AlertDialog 或任何您想要的扩展类。然后你可以实现你的自定义对话框的一些功能。您可以制作 yesPerformed() 和 noPerformed() 方法,当您需要使用自定义对话框时,您可以简单地覆盖这些方法。

于 2013-09-19T10:02:59.130 回答
-1

为了避免为对话重写代码,我创建了一个自定义类,可以在任何 UI 活动中使用。

如下创建一个 customDialog 类,并通过 CustomDialog.ShowErrorDialog(errormsg,myActivity.this); 从任何活动中调用

或通过

CustomDialog.ShowErrorDialogNRedirect(errormsg,myActivity.this,redirectActivityClass);

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;


/*****  
 * 
 *    This is CustomDialog class which is used commonly  in all the UI Activity .
 *  1)ShowErrorDialog function  is called from any activity by: 
 *  --> CustomDialog.ShowErrorDialog(errormsg,myActivity.this);
 *  
 *  This Dialogue shows  error msg only.
 *  
 *  
 *  2)ShowErrorDialogNRedirect function  is called from any activity by: 
 *  --> CustomDialog.ShowErrorDialogNRedirect(errormsg,myActivity.this,redirectActivityClass); 
 *   This Dialogue shows  error msg and redirects from one UI Activity to another Activity.
 *  
 * @author Jerry Abraham Varghese 
 * @license  WTFPL (do whatever you want with this, nobody cares)
 */     


public class CustomDialog  {

    // (1) Show  Error  Dialog
 @SuppressWarnings("deprecation")
public static void ShowErrorDialog(String errormsg ,final Activity activity)
 {
     AlertDialog alertDialog = new AlertDialog.Builder(activity).create();          
        // Setting Dialog Title
alertDialog.setTitle("Alert ");         
// Setting Dialog Message
alertDialog.setMessage(errormsg);           
// Setting Icon to Dialog
//alertDialog.setIcon(R.drawable.tick);         
// Setting OK Button            
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) {
 // Write your code here to execute after dialog closed
// Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
     }
});         
// Showing Alert Message
        alertDialog.show(); 
 }

 // (2) Show  Error  Dialog &  Redirect After Showing the Dialogue
 @SuppressWarnings({ "deprecation", "rawtypes" })
 public static void ShowErrorDialogNRedirect(String errormsg,final Activity activity,final Class nextRedirectActivityClass) {        
     AlertDialog alertDialog = new AlertDialog.Builder(activity).create();  
     // Setting Dialog Title
 alertDialog.setTitle(" "); 
 // Setting Dialog Message
 alertDialog.setMessage(errormsg);  
 // Setting Icon to Dialog
 alertDialog.setIcon(R.drawable.companylogo);   
 //On touch outside does not allow anything by setting "setCanceledOnTouchOutside" to false
 alertDialog.setCanceledOnTouchOutside(false);
 //In order to avoid the back pressed button not  to work when Dialog is open below code:
 alertDialog.setCancelable(false);   
 // Setting OK Button   
 alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
 public void onClick(DialogInterface dialog, int which) {
 // Write your code here to execute after dialog closed
     Intent nextActivity= new Intent(activity,nextRedirectActivityClass);
     activity.startActivity(nextActivity);
     }
});     
// Showing Alert Message
alertDialog.show();  
 }//End function of ShowErrorDialog()   


}
于 2014-03-11T06:41:02.517 回答