我当前有一个显示在片段上的警报对话框。该对话框显示给用户时没有问题,但“setPositiveButton”和“setNegativeButton”没有正确响应用户交互。在按下“setPositiveButton”后,Log.i是唯一实际工作的东西。我想让它显示一个吐司消息,并在按下possitive按钮后转到一个新的片段。
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();
}
});
}
1条答案
按热度按时间e1xvtsh31#
抱歉打扰各位,代码都很好。我开始使用真实的的设备而不是Android模拟器,一切都运行得很好。