Tôi đang cố gắng tìm cách sử dụng lại Hộp thoại hiển thị tiêu đề tùy chỉnh, sau đó gửi Có/Không nhấp vào chức năng đã khởi chạy Hộp thoại.Tôi làm cách nào để có thể sử dụng lại AlertDialog cho Có/Không trên Android?
Tôi có hai phần phụ, Lưu và Loại bỏ, và cả hai đều gọi một Có/Không Dialog, một hiển thị "Bạn có muốn lưu" và "Loại bỏ thay đổi" không?
Tôi nghĩ quy trình của tôi rất "bẩn" nhưng tôi đoán nó có thể hoạt động, nhưng vấn đề của tôi là biến "Chế độ xem chế độ xem", tôi không biết cách chuyển nó từ Hoạt động sang Hộp thoại, vì vậy tôi có thể sử dụng nó để gọi lại hàm đã khởi chạy Hộp thoại.
Cảm ơn trước, HerniHdez
java hoạt động của tôi (mảnh vỡ của nó)
public void open_HH_Fragment_YesNo(View view, String aux_title, String aux_function)
{
Bundle bundle=new Bundle();
bundle.putString("setMessage", aux_title);
bundle.putString("callingFunction", aux_function);
DialogFragment newFragment = new HH_Fragment_YesNo();
newFragment.setArguments(bundle);
newFragment.show(getSupportFragmentManager(), "HH_Fragment_YesNo");
}
public void SaveChanges(View view, String aux_YesNo)
{
if (aux_YesNo == "")
{
Toast.makeText(this, "Save changes?", Toast.LENGTH_SHORT).show();
open_HH_Fragment_YesNo(view, "Save changes?", "SaveChanges");
}
else if (aux_YesNo == "Yes")
{
Toast.makeText(this, "Saving changes", Toast.LENGTH_SHORT).show();
}
else if (aux_YesNo == "No")
{
Toast.makeText(this, "Save Cancelled", Toast.LENGTH_SHORT).show();
}
}
public void DismissChanges(View view, String aux_YesNo)
{
if (aux_YesNo == "")
{
Toast.makeText(this, "Dismiss changes?", Toast.LENGTH_SHORT).show();
open_HH_Fragment_YesNo(view, "Dismiss changes?", "DismissChanges");
}
else if (aux_YesNo == "Yes")
{
Toast.makeText(this, "Dismiss OK", Toast.LENGTH_SHORT).show();
Close(view);
}
else if (aux_YesNo == "No")
{
Toast.makeText(this, "Dismiss Cancelled", Toast.LENGTH_SHORT).show();
}
}
// The dialog fragment receives a reference to this Activity through the
// Fragment.onAttach() callback, which it uses to call the following methods
// defined by the HH_Fragment_YesNo.YesNoDialogListener interface
@Override
public void onDialogPositiveClick(DialogFragment dialog, View view, String aux_function)
{
// User touched the dialog's positive button
Toast.makeText(this, "User clicked on Yes", Toast.LENGTH_SHORT).show();
if (aux_function == "SaveChanges")
{
SaveChanges(view, "Yes");
}
else if (aux_function == "DismissChanges")
{
DismissChanges(view, "Yes");
}
}
@Override
public void onDialogNegativeClick(DialogFragment dialog, View view, String aux_function)
{
Toast.makeText(this, "User clicked on NO", Toast.LENGTH_SHORT).show();
if (aux_function == "SaveChanges")
{
SaveChanges(view, "No");
}
else if (aux_function == "DismissChanges")
{
DismissChanges(view, "No");
}
}
java của Dialog của tôi (hoàn thành)
public class HH_Fragment_YesNo extends DialogFragment
{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
String setMessage = getArguments().getString("setMessage");
final String callingFunction = getArguments().getString("callingFuntion");
builder
.setMessage(setMessage) // R.string.dialog_fire_missiles
.setPositiveButton("Sí", new DialogInterface.OnClickListener() // R.string.fire
{
public void onClick(DialogInterface dialog, int id)
{
// Exit without saving
mListener.onDialogPositiveClick(HH_Fragment_YesNo.this, view, callingFunction);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() // R.string.cancel
{
public void onClick(DialogInterface dialog, int id)
{
// User cancelled the dialog
mListener.onDialogNegativeClick(HH_Fragment_YesNo.this, view, callingFunction);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface YesNoDialogListener
{
public void onDialogPositiveClick(DialogFragment dialog, View view, String aux_Function);
public void onDialogNegativeClick(DialogFragment dialog, View view, String aux_Function);
}
// Use this instance of the interface to deliver action events
YesNoDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try
{
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (YesNoDialogListener) activity;
}
catch (ClassCastException e)
{
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
}
Cảm ơn Biraj, Tôi không chắc chắn về nơi tôi phải đặt mọi phần của mã. * Giao diện có nằm trong một tệp khác hoặc trong tệp java của hoạt động không? –
Tôi khuyên bạn nên đặt nó dưới dạng tệp riêng trong gói của bạn. –
Tôi đoán "Phương pháp tổng quát cho hộp thoại xác nhận" nằm trong tệp java của Hộp thoại và "cách sử dụng" trong tệp hoạt động, phải không? –