现在的位置: 首页 > 操作系统 > 正文

Android中的AlertDialog使用示例二(普通选项对话框)

2020年02月12日 操作系统 ⁄ 共 1448字 ⁄ 字号 评论关闭

在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式。下面我们简单模拟一个选花魁的简单普通选项(单选)对话框,如下图:

Layout界面代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="普通选项" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="show2" android:id="@+id/button2" /></LinearLayout>

Java功能实现代码:

public class AlertDialogDemo extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alertdialog); } public void show2(View v){ //实例化建造者 AlertDialog.Builder builder = new AlertDialog.Builder(this); //设置警告对话框标题 builder.setTitle("选花魁喽"); //定义警告框选择窗体的内容(这里用String数组) final String[] beautifulGirls = {"芙蓉姐姐","凤姐","范爷","我自己"}; //将数组内的元素设置到dialog的每个item中,并做事件处理(这里用土司) builder.setItems(beautifulGirls, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(AlertDialogDemo.this,beautifulGirls[which]+"被选中啦",Toast.LENGTH_SHORT).show(); } }); //显示对话框 builder.show(); }}

本文永久更新链接地址:http://www.xuebuyuan.com/Linux/2016-12/138654.htm

以上就上有关Android中的AlertDialog使用示例二(普通选项对话框)的相关介绍,要了解更多AlertDialog对话框,AlertDialog,Android中的AlertDialog使用示例二(普通选项对话框),编程,Linux编程,Linux Shell,Android,Android教程,JAVA,C语言,Python,HTML5内容请登录学步园。

抱歉!评论已关闭.