话不多说,上代码,实现了alertdialog实现简单的删除确认功能
//3.调用接口 public class MainActivity extends AppCompatActivity implements View.OnClickListener { //1.声明按钮控件 private Button btndelete; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //2.找到控件 btndelete = findViewById(R.id.delete); //4.添加点击事件 btndelete.setOnClickListener(this); } //调用解控后自动生成方法 @Override public void onClick(View v) { //5.调用AlerDialog静态内部类的构造方法 AlertDialog.Builder builder = new AlertDialog.Builder(this); //setIcon()为设置图标的方法 builder.setTitle("提示").setIcon(R.mipmap.dialog).setMessage("请确定是否删除?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); //创建alertdialog AlertDialog ad = builder.create(); //显示alertdialog ad.show(); } }