onclick 在 android 中的自定义 AlertDialog 中不起作用
onclick not working in custom AlertDialog in android
你好,在下面的代码中,如果它是空的,点击保存应该显示一些错误,否则应该移动到下一个 step.but 它不起作用。在 frgament 中,我正在调用 openDiag() 方法。
谁能帮帮我
public class HomeNurseFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_coordinatorhome, container, false);
openDialog(rootView);
public void openDialog(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
// alertDialogBuilder.setTitle("Select Hospital and Proceed");
final View customLayout
= getLayoutInflater()
.inflate(
R.layout.popup_hospitallist,
null);
alertDialogBuilder.setView(customLayout);
Button save = customLayout.findViewById(R.id.proceed);
TextView cancel = customLayout.findViewById(R.id.cancel);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(hospital.getItemAtPosition(0).equals("Preferred hospital")){
hospital.setError("Please select Preferred hospital ");
}else {
alertDialogBuilder.setCancelable(true);
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialogBuilder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
}
});
}
});
// AlertDialog alertDialog = alertDialogBuilder.create();
alertDialogBuilder.show();
}
}
}
如果您想显示一条错误消息而不是关闭 AlertDialog,则必须禁用 clickListener 并改为设置自定义的。
public void openDialog(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
// alertDialogBuilder.setTitle("Select Hospital and Proceed");
final View customLayout = getLayoutInflater().inflate(R.layout.popup_hospitallist, null);
alertDialogBuilder.setView(customLayout);
alertDialogBuilder.setPositiveButton("Save", null); // set listener to null as you will override it later
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
// Overriding the Positive button here
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(hospital.getItemAtPosition(0).equals("Preferred hospital")){
hospital.setError("Please select Preferred hospital ");
} else {
alertDialogBuilder.setCancelable(true);
}
}
});
你好,在下面的代码中,如果它是空的,点击保存应该显示一些错误,否则应该移动到下一个 step.but 它不起作用。在 frgament 中,我正在调用 openDiag() 方法。
谁能帮帮我
public class HomeNurseFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_coordinatorhome, container, false);
openDialog(rootView);
public void openDialog(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
// alertDialogBuilder.setTitle("Select Hospital and Proceed");
final View customLayout
= getLayoutInflater()
.inflate(
R.layout.popup_hospitallist,
null);
alertDialogBuilder.setView(customLayout);
Button save = customLayout.findViewById(R.id.proceed);
TextView cancel = customLayout.findViewById(R.id.cancel);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(hospital.getItemAtPosition(0).equals("Preferred hospital")){
hospital.setError("Please select Preferred hospital ");
}else {
alertDialogBuilder.setCancelable(true);
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialogBuilder.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
}
});
}
});
// AlertDialog alertDialog = alertDialogBuilder.create();
alertDialogBuilder.show();
}
}
}
如果您想显示一条错误消息而不是关闭 AlertDialog,则必须禁用 clickListener 并改为设置自定义的。
public void openDialog(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
// alertDialogBuilder.setTitle("Select Hospital and Proceed");
final View customLayout = getLayoutInflater().inflate(R.layout.popup_hospitallist, null);
alertDialogBuilder.setView(customLayout);
alertDialogBuilder.setPositiveButton("Save", null); // set listener to null as you will override it later
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
// Overriding the Positive button here
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(hospital.getItemAtPosition(0).equals("Preferred hospital")){
hospital.setError("Please select Preferred hospital ");
} else {
alertDialogBuilder.setCancelable(true);
}
}
});