• Android中的AlertDialog使用示例三(单向选择确定对话框)


    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式。下面我们简单模拟一个挑媳妇的选择确定对话框(单选)对话框,不同于示例二之处在于本次只要不确定就可以后悔哦,如下图:

     

    Layout界面代码:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     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="show3"
    10         android:id="@+id/button3" />
    11 </LinearLayout>

     

    Java功能实现代码:

     1 public class AlertDialogDemo extends AppCompatActivity {
     2     int index=0;
     3     @Override
     4     protected void onCreate(@Nullable Bundle savedInstanceState) {
     5         super.onCreate(savedInstanceState);
     6         setContentView(R.layout.alertdialog);
     7     }
     8     public void show3(View v){
     9         AlertDialog.Builder builder = new AlertDialog.Builder(this);
    10         builder.setTitle("挑媳妇儿");
    11         final String[] beautifulGirls = {"芙蓉姐姐","凤姐","范爷","我自己"};
    12         builder.setSingleChoiceItems(beautifulGirls, 0, new DialogInterface.OnClickListener() {
    13             @Override
    14             public void onClick(DialogInterface dialog, int which) {
    15                 index = which;
    16             }
    17         });
    18         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    19             @Override
    20             public void onClick(DialogInterface dialog, int which) {
    21                 Toast.makeText(AlertDialogDemo.this,beautifulGirls[index]+"是你的媳妇了",Toast.LENGTH_SHORT).show();
    22             }
    23         });
    24         builder.show();
    25     }
    26 }

    优化空间很大,大家都可以玩玩。

  • 相关阅读:
    python CST中国标准时间格式转换
    pytest+报告插件
    getopt实现命令行
    初识Redis
    python list 切片及翻转的使用
    mysql中information_schema表
    获取两个字符串中最长的相同子串
    mongodb数据库备份
    VS2005下边不能使用target>remote tool解决方法
    wince LoadDriver tool
  • 原文地址:https://www.cnblogs.com/panhouye/p/6099678.html
Copyright © 2020-2023  润新知