我有这个警报对话框,它有两个按钮(确定和取消)。我想知道如何实施它。
因此,当您单击取消按钮时,它应该关闭警报对话框并返回到我当前所在的片段。如果我点击“确定”按钮,它应该替换当前的警告对话框并将其与另一个对话框放置。
这是我的下面的代码用于confimration。 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>
请有人帮助我
答案 0 :(得分:3)
尝试使用此代码来实现上述功能:
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();
答案 1 :(得分:1)
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("your message ");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); //<-- change it with ur code
}
} );
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
} );
alertDialog.show();
答案 2 :(得分:0)
builder.setPositiveButton(text, new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}).setNegativeButton(text, new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}).create();