星空网 > 软件开发 > 操作系统

Android中的AlertDialog(警告对话框)

Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式。下面我们模拟卸载应用程序时弹出的最为普通的警告对话框,如下图:

Android中的AlertDialog(警告对话框)images/loading.gif' data-original="http://images2015.cnblogs.com/blog/1068139/201611/1068139-20161122235921409-51039050.gif" >

layout布局界面代码示例:

 1 <? 2 <LinearLayout  3   android:orientation="vertical" android:layout_width="match_parent" 4   android:layout_height="match_parent"> 5   <Button 6     android:text="卸载" 7     android:layout_width="match_parent" 8     android:layout_height="wrap_content" 9     android:onClick="show"10     android:id="@+id/button" />11 </LinearLayout>

Java实现代码:

 1 import android.content.DialogInterface; 2 import android.os.Bundle; 3 import android.support.annotation.Nullable; 4 import android.support.v7.app.AlertDialog; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.Toast; 8 /** 9  * Created by panchengjia on 2016/11/21.10 */11 public class AlertDialogDemo extends AppCompatActivity {12   @Override13   protected void onCreate(@Nullable Bundle savedInstanceState) {14     super.onCreate(savedInstanceState);15     setContentView(R.layout.alterdialog);16   }17   public void show(View v){18     //实例化建造者19     AlertDialog.Builder builder = new AlertDialog.Builder(this);20     //设置警告对话框的标题21     builder.setTitle("卸载");22     //设置警告显示的图片23 //    builder.setIcon(android.R.drawable.ic_dialog_alert);24     //设置警告对话框的提示信息25     builder.setMessage("确定卸载吗");26     //设置”正面”按钮,及点击事件27     builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {28       @Override29       public void onClick(DialogInterface dialog, int which) {30         Toast.makeText(AlertDialogDemo.this,"点击了确定按钮",Toast.LENGTH_SHORT).show();31       }32     });33     //设置“反面”按钮,及点击事件34     builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {35       @Override36       public void onClick(DialogInterface dialog, int which) {37         Toast.makeText(AlertDialogDemo.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();38       }39     });40     //设置“中立”按钮,及点击事件41     builder.setNeutralButton("等等看吧", new DialogInterface.OnClickListener() {42       @Override43       public void onClick(DialogInterface dialog, int which) {44         Toast.makeText(AlertDialogDemo.this,"点击了中立按钮",Toast.LENGTH_SHORT).show();45       }46     });47     //显示对话框48     builder.show();49   }50 }

 




原标题:Android中的AlertDialog(警告对话框)

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流