我正在制作一个应用程序,其中我需要多次显示对话框,我不想一次又一次地编写对话框的代码,所以我应该如何创建一个通用对话框以便我可以执行不同的功能在不同的地方选择是或否。请帮助,我尝试了很多,但没有得到解决方案。
答案 0 :(得分: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);
答案 1 :(得分: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();
}
答案 2 :(得分:0)
您可以尝试从Dialog / AlertDialog或任何您想要的扩展类。然后,您可以实现自定义对话框的某些功能。您可以使用yesPerformed()和noPerformed()方法,当您需要使用自定义DialogBox时,您可以简单地覆盖。
答案 3 :(得分:0)
我只是把我现成的代码贴在我的项目上。请相应修改不同的标签或向我寻求进一步的帮助!
这是你的对话框的样子:
第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();
答案 4 :(得分:0)
我知道这已经很久了 但作为参考,您可以在
中找到一个简单的答案Android Confirmation dialog returning true or false
查找已接受的答案,并查看"技巧"
答案 5 :(得分:-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()
}