我正在学习并记住Android,我想了解我的错误。 这是我的确认/取消AlertDialog类。在单独的类中创建它以分割代码,而不是将所有内容放在一个Activity中:
public class ConversationAlertDialog extends DialogFragment {
public static Dialog createDialog(Context context) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Continue?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User confirms
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
这是我显示AlertDialog的活动:
buttonConvert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ConversationAlertDialog.createDialog(ConversionActivity.this).show();
}
});
接下来我想创建回调,这样我就可以在Activity中编写Dialog取消/确认行为。这样,使用已在Activity中编写的代码也会更容易。我还在学习回调,所以不确定,但我认为我应该实现接口。
所有类型的提示都会对我有所帮助。
答案 0 :(得分:1)
在ConversationAlertDialog
课程中创建一个界面。
public interface onActionSelect{
void onOkSelect();
void onCancelSelect();
}
在Activity上实现它并将其传递给ConversationAlertDialog
类。然后从对话框中调用。
// For Positive Button Click
public void onClick(DialogInterface dialog, int id) {
// User confirms
if(mCallBack != null) mCallBack.onOkSelect()
}
// For Negative Button Click
public void onClick(DialogInterface dialog, int id) {
// User confirms
if(mCallBack != null) mCallBack.onCancelSelect()
}
单击正面或负面按钮时,您将在MainActivity中收到通知。
答案 1 :(得分:1)
public class ConversationAlertDialog extends DialogFragment {
private onMyEvent event;
public void setMyEvent(onMyEvent e)
{
this.event=e;
}
public static Dialog createDialog(Context context) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Continue?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User confirms
if(null!=event)
{
event.ok();
}
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if(null!=event)
{
event.cancel();
}
// User cancelled
}
});
// Create the AlertDialog object and return it
return builder.create();
}
public interface onMyEvent{
void ok();
void cancel();
}
}
buttonConvert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Dialog dialog=ConversationAlertDialog.createDialog(ConversionActivity.this);
dialog.setMyEvent(new onMyEvent{....})
dialog.show();
}
});
答案 2 :(得分:1)
他是我使用的代码。我发现它非常灵活,因为您可以为略有不同的任务创建几个类似的对话框。您需要创建一个布局文件 - 这为您提供了很大的功能和风格灵活性。
我的布局文件是fragment_ok_cancel_dialog
。
在调用对话框的Activity中,您需要实现Listener。
implements OkCancelDialogFragment.OkCancelDialogListener
另一个优点是我的代码可以更改标题和消息以满足任何活动的需要。
private void callMyDialog(){
//Customize the title and message as needed
String title = "This is my dialog title";
String mess = "This is my dialog message";
OkCancelDialogFragment dialog = OkCancelDialogFragment.newInstance(title, mess);
dialog.show(getFragmentManager(), "OkCancelDialogFragment2");
}
现在,您需要在调用DialogFragment的Activity中实现对话框回调。
@Override
public void onFinishOkCancelDialog(boolean submit) {
if(submit){
// Do something positive
}
else{
// Do something negative
}
}
现在是DialogFragment
的代码:
public class OkCancelDialogFragment extends DialogFragment {
private static final String ARG_TITLE = "title";
private static final String ARG_MESSAGE = "message";
Context context = null;
private String title;
private String message;
private boolean submitData = false;
private OkCancelDialogListener mListener;
public OkCancelDialogFragment() {
}
public static OkCancelDialogFragment newInstance(String title, String message) {
OkCancelDialogFragment fragment = new OkCancelDialogFragment();
Bundle args = new Bundle();
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, message);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
title = getArguments().getString(ARG_TITLE);
message = getArguments().getString(ARG_MESSAGE);
}
}
@Override
public Dialog onCreateDialog(Bundle saveIntsanceState){
context = getActivity();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_ok_cancel_dialog, null, false);
final TextView titleView = (TextView)rootView.findViewById(R.id.tvTitle);
final TextView messView = (TextView)rootView.findViewById(R.id.tvMessage);
titleView.setText(title);
messView.setText(message);
builder.setView(rootView)
// .setTitle(title)
.setPositiveButton(R.string.ok_button_dialog_title, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
submitData = true;
if(mListener == null) mListener = (OkCancelDialogListener) context;
mListener.onFinishOkCancelDialog(submitData);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
submitData = false;
if(mListener == null) mListener = (OkCancelDialogListener) context;
mListener.onFinishOkCancelDialog(submitData);
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
if(mListener == null) mListener = (OkCancelDialogListener) context;
}
catch (Exception ex){
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OkCancelDialogListener {
void onFinishOkCancelDialog(boolean submit);
}
}
请注意,.setTitle(title)
适用于API 23或更高版本(或者可能是API 21或更高版本?)。