<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.hanqi.testapp2.Xiti2duihuakuangActivity" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="一般对话框" android:onClick="sb1_OnClick"/> </LinearLayout>
package com.hanqi.testapp2; import android.app.AlertDialog; import android.content.DialogInterface; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class Xiti2duihuakuangActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_xiti2duihuakuang); } public void sb1_OnClick(View v) { AlertDialog alertDialog=new AlertDialog.Builder(this) .setTitle("提示") .setMessage("确定要删除吗?要删除,请点击“是”") .setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Xiti2duihuakuangActivity.this, "执行删除,which="+which, Toast.LENGTH_SHORT).show(); } }) .setNegativeButton("否", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(Xiti2duihuakuangActivity.this, "取消删除,which="+which, Toast.LENGTH_SHORT).show(); } }) .setCancelable(false) .show(); } }