删除对话框练习

一.写出实现如图所示的对话框的代码。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.example.wang.xuexi.TestActivity1"
11     android:orientation="vertical">
12 
13     <Button
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="删除"
17         android:onClick="bt1_OnClick"/>
18 </LinearLayout>
.xml
 1 package com.example.wang.xuexi;
 2 
 3 import android.app.AlertDialog;
 4 import android.content.DialogInterface;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Toast;
 9 
10 public class TestActivity1 extends AppCompatActivity {
11 
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_test1);
16 
17     }
18 
19     public  void bt1_OnClick(View v)
20     {
21 
22         AlertDialog alertDialog=new AlertDialog.Builder(this)
23                 .setTitle("提示")
24                 .setMessage("确定要删除吗?"+"
"+
25                         "要删除,请点击“是”。")
26                 .setPositiveButton("是", new DialogInterface.OnClickListener() {
27                     @Override
28                     public void onClick(DialogInterface dialog, int which) {
29 
30                         Toast.makeText(TestActivity1.this, "已删除,which" + which, Toast.LENGTH_SHORT).show();
31 
32                     }
33                 })
34                 .setNegativeButton("否", new DialogInterface.OnClickListener() {
35                     @Override
36                     public void onClick(DialogInterface dialog, int which) {
37 
38                         Toast.makeText(TestActivity1.this, "未删除,which" + which, Toast.LENGTH_SHORT).show();
39 
40                     }
41                 })
42                 .show();
43     }
44 }
.java

原文地址:https://www.cnblogs.com/arxk/p/5491588.html