我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
我想显示一个对话框/弹出窗口,向用户显示一条消息“您确定要删除此条目吗?”,其中一个按钮说“删除”。当点击Delete时,它应该删除该条目,否则什么都不删除。
我已经为这些按钮写了一个点击监听器,但我如何调用对话框或弹出窗口及其功能?
当前回答
在材质组件库中,你可以使用MaterialAlertDialogBuilder
MaterialAlertDialogBuilder(context)
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete") { dialog, which ->
// Respond to positive button press
}
.setNegativeButton("Cancel") { dialog, which ->
// Respond to positive button press
}
.show()
使用Compose 1.0。X你可以使用:
val openDialog = remember { mutableStateOf(true) }
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
// Dismiss the dialog when the user clicks outside the dialog or on the back
// button. If you want to disable that functionality, simply use an empty
// onCloseRequest.
openDialog.value = false
},
title = null,
text = {
Text(
"Are you sure you want to delete this entry?"
)
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Delete")
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Cancel")
}
}
)
}
其他回答
我已经创建了一个对话框,用于询问Person是否想呼叫Person。
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.Toast;
public class Firstclass extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
ImageView imageViewCall = (ImageView) findViewById(R.id.ring_mig);
imageViewCall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
try
{
showDialog("0728570527");
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
public void showDialog(final String phone) throws Exception
{
AlertDialog.Builder builder = new AlertDialog.Builder(Firstclass.this);
builder.setMessage("Ring: " + phone);
builder.setPositiveButton("Ring", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Intent callIntent = new Intent(Intent.ACTION_DIAL);// (Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phone));
startActivity(callIntent);
dialog.dismiss();
}
});
builder.setNegativeButton("Avbryt", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
});
builder.show();
}
}
new AlertDialog.Builder(loginregister.this)
.setTitle("messege")
.setPositiveButton("ok", null)
.setMessage( "user name : " + username + "/n" +
"password :" + password + "/n" )
.show();
从列表中删除条目的代码
/*--dialog for delete entry--*/
private void cancelBookingAlert() {
AlertDialog dialog;
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialogCustom);
alertDialog.setTitle("Delete Entry");
alertDialog.setMessage("Are you sure you want to delete this entry?");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//code to delete entry
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = alertDialog.create();
dialog.show();
}
在删除按钮单击时调用上述方法
你也可以尝试这种方式,它将为你提供材料样式的对话框
private void showDialog()
{
String text2 = "<font color=#212121>Medi Notification</font>";//for custom title color
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle(Html.fromHtml(text2));
String text3 = "<font color=#A4A4A4>You can complete your profile now or start using the app and come back later</font>";//for custom message
builder.setMessage(Html.fromHtml(text3));
builder.setPositiveButton("DELETE", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
toast = Toast.makeText(getApplicationContext(), "DELETE", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
toast = Toast.makeText(getApplicationContext(), "CANCEL", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
});
builder.show();
}
在材质组件库中,你可以使用MaterialAlertDialogBuilder
MaterialAlertDialogBuilder(context)
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton("Delete") { dialog, which ->
// Respond to positive button press
}
.setNegativeButton("Cancel") { dialog, which ->
// Respond to positive button press
}
.show()
使用Compose 1.0。X你可以使用:
val openDialog = remember { mutableStateOf(true) }
if (openDialog.value) {
AlertDialog(
onDismissRequest = {
// Dismiss the dialog when the user clicks outside the dialog or on the back
// button. If you want to disable that functionality, simply use an empty
// onCloseRequest.
openDialog.value = false
},
title = null,
text = {
Text(
"Are you sure you want to delete this entry?"
)
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Delete")
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text("Cancel")
}
}
)
}