gpt4 book ai didi

java - 在对话框 fragment 中打开 AlertDialog

转载 作者:行者123 更新时间:2023-12-01 19:32:22 25 4
gpt4 key购买 nike

我有一个自定义对话框 fragment ,我需要在其中显示一个警报对话框

是否可以在不关闭对话框 fragment 的情况下完成?
当我使用为对话框 fragment 提供的上下文执行此操作时,它只是关闭对话框 fragment 并打开警报对话框 >.

case R.id.deleteButton:
new AlertDialog.Builder(context)
.setTitle(context.getResources().getString(R.string.discard_changes))
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, (dialog, whichButton) -> {
onItemClickListener.onItemClick(Device, DELETE_DOC);
dismiss();
})
.setNegativeButton(android.R.string.no, null).show();
break;

编辑

根据要求,整个对话框代码:

public class InstallationDocumentDeviceAddFormDialog extends Dialog implements View.OnClickListener {

private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
private Context context;

private Button btn_cancel;

NavBar navBar;

Button addDeviceButton;

ImageButton deleteButton;

boolean isEditMode;

public static int EDIT_INSTALLATIONDOC = 1;
public static int DELETE_INSTALLATIONDOC = 2;

private InstallationDocumentDevice installationDocumentDevice;

public interface OnItemClickListener {
void onItemClick(InstallationDocumentDevice installationDocumentDevice, int action);
}

@NonNull
private OnItemClickListener onItemClickListener;

public InstallationDocumentDeviceAddFormDialog(@NonNull Context context, InstallationDocumentDevice installationDocumentDevice, boolean isEditMode, @NonNull OnItemClickListener onItemClickListener) {
super(context);
this.context = context;
this.onItemClickListener = onItemClickListener;
this.installationDocumentDevice = installationDocumentDevice;
this.isEditMode = isEditMode;

Log.i("DocumentController", "dialog device: " + installationDocumentDevice.toString());
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.add_new_device_form);

RecyclerView accessoriesRecyclerView = (RecyclerView) findViewById(R.id.addNewDeviceAccessoriesRecycler);
RecyclerView guidancesRecyclerView = (RecyclerView) findViewById(R.id.addNewDeviceGuidancesRecycler);
RecyclerView testsRecyclerView = (RecyclerView) findViewById(R.id.addNewDeviceTestsRecycler);

navBar = findViewById(R.id.navBar);
if(isEditMode) navBar.setTitle(context.getResources().getString(R.string.editing_device) + " " + installationDocumentDevice.getLocalizedDeviceName());
else navBar.setTitle(context.getResources().getString(R.string.adding_new_device) + " " + installationDocumentDevice.getLocalizedDeviceName());

addDeviceButton = findViewById(R.id.btnAddNewDevice);
addDeviceButton.setOnClickListener(this);
deleteButton = (ImageButton) findViewById(R.id.deleteButton);

if(isEditMode){
deleteButton.setVisibility(View.VISIBLE);
deleteButton.setOnClickListener(this);
addDeviceButton.setText(context.getResources().getString(R.string.add_new_device_title));
} else {
deleteButton.setVisibility(View.GONE);
}

// navBar.getBackButton().setOnClickListener(view -> dismiss());


accessoriesRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
guidancesRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
testsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

accessoriesRecyclerView.setAdapter(new InstallationDocumentDeviceAddFormAdapter(installationDocumentDevice, this::onClick, InstallationDocumentDeviceAddFormAdapter.ACCESSORIES));
guidancesRecyclerView.setAdapter(new InstallationDocumentDeviceAddFormAdapter(installationDocumentDevice, this::onClick, InstallationDocumentDeviceAddFormAdapter.GUIDANCES));
testsRecyclerView.setAdapter(new InstallationDocumentDeviceAddFormAdapter(installationDocumentDevice, this::onClick, InstallationDocumentDeviceAddFormAdapter.TESTS));

}

@Override
public void onClick(View v) {
switch (v.getId()) {

case R.id.btnAddNewDevice:
onItemClickListener.onItemClick(installationDocumentDevice, EDIT_INSTALLATIONDOC);
dismiss();
break;

case R.id.deleteButton:
new AlertDialog.Builder(context)
.setTitle(context.getResources().getString(R.string.datapick_discard_changes))
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton(android.R.string.yes, (dialog, whichButton) -> {
onItemClickListener.onItemClick(installationDocumentDevice, DELETE_INSTALLATIONDOC);
dismiss();
})
.setNegativeButton(android.R.string.no, null).show();
// onItemClickListener.onItemClick(installationDocumentDevice, DELETE_INSTALLATIONDOC);
// dismiss();
break;
}
dismiss();
}

private void onClick(int position, int tag) {
switch (tag) {
//Case Accessory
case 1:
// Toast.makeText(context, "Accessory pos: " + position + ". Is checked: " + installationDocumentDevice.getAccessories().get(position).isChecked(), Toast.LENGTH_SHORT).show();
break;
//Case Guidance
case 2:
// Toast.makeText(context, "Guidances pos: " + position, Toast.LENGTH_SHORT).show();
break;
//Case Tests
case 3:
// Toast.makeText(context, "Tests pos: " + position, Toast.LENGTH_SHORT).show();
break;
}
}

}

最佳答案

/* 是的,这是可能的,您可以通过下面我的代码进行扩展,我在 Holder View 点击事件的回收器适配器中使用该代码。*/

     holder.calendar_day_gridcell.setOnClickListener(new View.OnClickListener() 
{
@Override
public void onClick(View v1) {
// holder.txt_InTime.setText("Helllo");

try {
// Date parsedDate = dateFormatter.parse(date_month_year);
//Log.d(tag, "Parsed Date: " + parsedDate.toString());

DoubleClickPerform.preventTwoClick(v1);

final Dialog dialog = new Dialog(context,R.style.Theme_AppCompat_Dialog_Alert);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Calendar calendar;
int currentHour;
int currentMinute;
TextView txt_dialog_InTime,txt_dialog_OutTime,txt_dialog_SaveInTime,/*txt_dialog_Delete_InTime*/txt_dialog_SaveOutTime/*txt_dialog_Delete_OutTime*/;
ImageView img_clean_in_time,img_clean_out_time;
Button btn_close;

View view1 = context.getLayoutInflater().inflate(R.layout.in_out_time_dialog, null);
txt_dialog_InTime = view1.findViewById(R.id.txt_dialog_InTime);
txt_dialog_OutTime = view1.findViewById(R.id.txt_dialog_OutTime);
txt_dialog_SaveInTime = view1.findViewById(R.id.txt_dialog_SaveInTime);
// txt_dialog_Delete_InTime = view1.findViewById(R.id.txt_dialog_Delete_InTime);
txt_dialog_SaveOutTime = view1.findViewById(R.id.txt_dialog_SaveOutTime);
// txt_dialog_Delete_OutTime = view1.findViewById(R.id.txt_dialog_Delete_OutTime);
img_clean_in_time = view1.findViewById(R.id.img_clean_in_time);
img_clean_out_time = view1.findViewById(R.id.img_clean_out_time);
btn_close = view1.findViewById(R.id.btn_close);
calendar = Calendar.getInstance();
currentHour = calendar.get(Calendar.HOUR_OF_DAY);
currentMinute = calendar.get(Calendar.MINUTE);
txt_dialog_InTime.setText(holder.txt_InTime.getText().toString());

if(txt_dialog_InTime.getText().toString().equals(""))
{
txt_dialog_OutTime.setEnabled(false);
}
else
{
txt_dialog_OutTime.setEnabled(true);

}
btn_close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
txt_dialog_InTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

TimePickerDialog timePickerDialog= new TimePickerDialog(context,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {


Time time = new Time(hour, minute, 0);

//little h uses 12 hour format and big H uses 24 hour format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm aa");

//format takes in a Date, and Time is a sublcass of Date
String s = simpleDateFormat.format(time);

txt_dialog_InTime.setText(s);
}
}, currentHour, currentMinute, false);
timePickerDialog.show();
}
});
dialog.setContentView(view1);
dialog.show();

img_clean_in_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt_dialog_InTime.setText("");

}
});
img_clean_out_time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txt_dialog_OutTime.setText("");

}
});

txt_dialog_OutTime.setText(holder.txt_OutTime.getText().toString());
txt_dialog_OutTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

TimePickerDialog timePickerDialog= new TimePickerDialog(context,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hour, int minute) {


Time time = new Time(hour, minute, 0);

//little h uses 12 hour format and big H uses 24 hour format
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm aa");

//format takes in a Date, and Time is a sublcass of Date
String s = simpleDateFormat.format(time);
txt_dialog_OutTime.setText(s);
}
}, currentHour, currentMinute, false);
timePickerDialog.show();
}
});
dialog.setContentView(view1);
dialog.show();


/* txt_dialog_Delete_InTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.txt_InTime.setText("");
dialog.dismiss();

}
});

txt_dialog_Delete_OutTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.txt_OutTime.setText("");
dialog.dismiss();


}
});
*/
txt_dialog_SaveInTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


holder.txt_InTime.setText(txt_dialog_InTime.getText().toString());

apiforSaveTime(txt_dialog_InTime.getText().toString(),txt_dialog_OutTime.getText().toString(), finalRevereseDate);
dialog.dismiss();
}
});

txt_dialog_SaveOutTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


holder.txt_OutTime.setText(txt_dialog_OutTime.getText().toString());

if(!txt_dialog_InTime.getText().toString().equals(""))
{
apiforSaveTime(txt_dialog_InTime.getText().toString(),txt_dialog_OutTime.getText().toString(), finalRevereseDate);
dialog.dismiss();

}
else
{
Snackbar snackbar = Snackbar
.make(holder.itemView, "In Time Is Required", Snackbar.LENGTH_LONG);

snackbar.show();
}


}
});
} catch (Exception e) {
e.printStackTrace();
}



}
});

关于java - 在对话框 fragment 中打开 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59249168/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com