gpt4 book ai didi

java - 警报对话框 - 如何实现“取消”和“确定”按钮

转载 作者:行者123 更新时间:2023-12-01 07:56:48 24 4
gpt4 key购买 nike

我有一个警报对话框,其中有两个按钮(确定和取消)。我想知道如何实现它。

因此,当您单击取消按钮时,它应该关闭警报对话框并返回到我当前所在的 fragment 。如果我单击“确定”按钮,它应该替换当前的警报对话框并将其替换为另一个警报对话框。

这是我下面的确认代码。 java 文件;

public class confirmation extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inf = getActivity().getLayoutInflater();
final View theDIalog = inf.inflate(R.layout.example_xml, null);
builder.setView(theDIalog);

AlertDialog dialog = builder.create();



theDIalog.findViewById(R.id.makeaTransferOk).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Okay button is clicked", Toast.LENGTH_SHORT).show();
}
});


theDIalog.findViewById(R.id.makeaTransferCancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

return dialog;
}
}

这是我的 example_xml 代码;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffc0c0c0">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/makeaTransferCancel"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/makeaTransferOk"/>

</RelativeLayout>

请有人帮助我

最佳答案

尝试使用此代码来实现上面提到的功能:

AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//put your code that needed to be executed when okay is clicked
dialog.cancel();


}
});
builder1.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

AlertDialog alert11 = builder1.create();
alert11.show();

关于java - 警报对话框 - 如何实现“取消”和“确定”按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29083115/

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