AlertDialog "onClick" 方法无法正常工作
AlertDialog "onClick" method not working properly
我目前有一个显示在片段上的警告对话框。该对话框向用户显示没有问题,但“setPositiveButton”和“setNegativeButton”没有正确响应用户交互,在“setPositiveButton”之后的Log.i被按下是唯一真正起作用的东西。我希望它显示 Toast 消息并在按下肯定按钮后移动到新片段。
private FragmentRequestCodeBinding binding;
//TAG
private static final String TAG = "requestCodeFragment";
//Values
private String firstLetter = "B";
private int secondLetter = 0;
private String thirdLetter = "0";
private String fourthLetter = "0";
private String fifthLetter = "0";
private String dtcCode = firstLetter + secondLetter + thirdLetter + fourthLetter + fifthLetter;
//Buttons
private Button request;
//Views
View root;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentRequestCodeBinding.inflate(inflater, container, false);
root = binding.getRoot();
request = root.findViewById(R.id.requestButton);
// Inflate the layout for this fragment
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final NavController navController = Navigation.findNavController(view);
request.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(getContext())
.setIcon(R.drawable.ic_baseline_priority_high_24)
.setTitle("Request for " + dtcCode)
.setMessage("Are you sure you want to request the following code " +
"for this car model?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Ticket created successfully!",
Toast.LENGTH_SHORT).show();
navController.navigate(R.id.action_requestCodeFragment_to_navigation_profile);
Log.i(TAG, "onClick: Pressed");
}
})
.setNegativeButton("No", null)
.setCancelable(true)
.show();
}
});
}
不好意思打扰大家了,代码没问题。我开始使用真实设备而不是 android 模拟器,一切正常。
我目前有一个显示在片段上的警告对话框。该对话框向用户显示没有问题,但“setPositiveButton”和“setNegativeButton”没有正确响应用户交互,在“setPositiveButton”之后的Log.i被按下是唯一真正起作用的东西。我希望它显示 Toast 消息并在按下肯定按钮后移动到新片段。
private FragmentRequestCodeBinding binding;
//TAG
private static final String TAG = "requestCodeFragment";
//Values
private String firstLetter = "B";
private int secondLetter = 0;
private String thirdLetter = "0";
private String fourthLetter = "0";
private String fifthLetter = "0";
private String dtcCode = firstLetter + secondLetter + thirdLetter + fourthLetter + fifthLetter;
//Buttons
private Button request;
//Views
View root;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentRequestCodeBinding.inflate(inflater, container, false);
root = binding.getRoot();
request = root.findViewById(R.id.requestButton);
// Inflate the layout for this fragment
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final NavController navController = Navigation.findNavController(view);
request.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(getContext())
.setIcon(R.drawable.ic_baseline_priority_high_24)
.setTitle("Request for " + dtcCode)
.setMessage("Are you sure you want to request the following code " +
"for this car model?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Ticket created successfully!",
Toast.LENGTH_SHORT).show();
navController.navigate(R.id.action_requestCodeFragment_to_navigation_profile);
Log.i(TAG, "onClick: Pressed");
}
})
.setNegativeButton("No", null)
.setCancelable(true)
.show();
}
});
}
不好意思打扰大家了,代码没问题。我开始使用真实设备而不是 android 模拟器,一切正常。