安卓弹出框集合

安卓弹出框集合

效果

  • 标题加内容

  • 经典弹出框
    安卓弹出框集合_第1张图片

  • 单机列表

  • 复选框

安卓弹出框集合_第2张图片
* 自定义对话框效果
安卓弹出框集合_第3张图片

实现源码

  • 标题加内容

      /**
         * 只有标题加内容
         * @param view
         */
        public void Btn1(View view) {
            // 1. Instantiate an AlertDialog.Builder with its constructor
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // 2. Chain together various setter methods to set the dialog characteristics
            builder.setMessage("我是内容")
                    .setTitle("我是标题");
            // 3. Get the AlertDialog from create()
            AlertDialog dialog = builder.create();
            //4. show
            dialog.show();
        }
    
  • 经典的弹出框

        /**
         * 经典的弹出框
         * @param view
         */
        public void Btn2(View view) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // Add the buttons
            builder.setMessage("我是内容")
                    .setTitle("我是标题")
                    .setIcon(R.mipmap.bulb)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User clicked OK button
                    Toast.makeText(MainActivity.this,"点击了确定按钮",Toast.LENGTH_SHORT).show();
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
                }
            });
            // Set other dialog properties
            // ...
            builder.setNeutralButton("中立按钮", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this,"点击了中立按钮",Toast.LENGTH_SHORT).show();
                }
            });
            // Create the AlertDialog
            AlertDialog dialog = builder.create();
            //show
            dialog.show();
        }
    
  • 单击列表,没有确认取消按钮

    /**
     * 单击列表,没有确认取消按钮
     * @param view
     */
    public void Btn3(View view) {
       final String[] arrs=new String[]{"red","block","gray","pcik"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("我是标题")
        .setItems(arrs, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // The 'which' argument contains the index position
                // of the selected item
                Toast.makeText(MainActivity.this,"点击了"+arrs[which],Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog dialog =builder.create();
        //show
        dialog.show();
    }
    
  • 复选框效果

         /**
         * 复选框效果
         * @param view
         */
        public void Btn4(View view) {
            final String[] arrs=new String[]{"red","block","gray","pcik"};
            final List mSelectedItems = new ArrayList();  // Where we track the selected items
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // Set the dialog title
            builder.setTitle("我是标题")
                    // Specify the list array, the items to be selected by default (null for none),
                    // and the listener through which to receive callbacks when items are selected
                    .setMultiChoiceItems(arrs, null,
                            new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which,
                                                    boolean isChecked) {
                                    if (isChecked) {
                                        // If the user checked the item, add it to the selected items
                                        mSelectedItems.add(which);
                                    } else if (mSelectedItems.contains(which)) {
                                        // Else, if the item is already in the array, remove it
                                        mSelectedItems.remove(Integer.valueOf(which));
                                    }
                                }
                            })
                    // Set the action buttons
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            // User clicked OK, so save the mSelectedItems results somewhere
                            // or return them to the component that opened the dialog
                           // ...
                            String str="";
                            for (int i = 0; i <mSelectedItems.size() ; i++) {
                                str+=arrs[i]+"/";
                            }
                            Toast.makeText(MainActivity.this,"点击了"+str,Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                           // ...
                            Toast.makeText(MainActivity.this,"点击了取消按钮",Toast.LENGTH_SHORT).show();
                        }
                    });
            AlertDialog dialog =builder.create();
            //show
            dialog.show();
        }
    
  • 自定义对话框,可以根据自己需求设置任何内容。这里的例子是一个登录界面

    • 布局文件dialog_signin.xml

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
          <ImageView
              android:src="@drawable/login"
              android:layout_width="match_parent"
              android:layout_height="64dp"
              android:scaleType="center"
              android:background="#FFFFBB33"
              android:contentDescription="@string/app_name" />
          <EditText
              android:id="@+id/username"
              android:inputType="textEmailAddress"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="16dp"
              android:layout_marginLeft="4dp"
              android:layout_marginRight="4dp"
              android:layout_marginBottom="4dp"
              android:hint="请输入用户名" />
          <EditText
              android:id="@+id/password"
              android:inputType="textPassword"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="4dp"
              android:layout_marginLeft="4dp"
              android:layout_marginRight="4dp"
              android:layout_marginBottom="16dp"
              android:fontFamily="sans-serif"
              android:hint="请输入密码"/>
      </LinearLayout>
      
    • activity内容

           /**
           * 自定义对话框,可以根据自己需求设置任何内容。这里的例子是一个登录界面
           * @param view
           */
          public void Btn5(View view) {
              AlertDialog.Builder builder = new AlertDialog.Builder(this);
              // Get the layout inflater
              LayoutInflater inflater = this.getLayoutInflater();
              // Inflate and set the layout for the dialog
              // Pass null as the parent view because its going in the dialog layout
              builder.setView(inflater.inflate(R.layout.dialog_signin, null))
                      // Add action buttons
                      .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int id) {
                              // sign in the user ...
                              Toast.makeText(MainActivity.this,"这里写登录成功的处理",Toast.LENGTH_SHORT).show();
                          }
                      })
                      .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                           //
                              Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
                          }
                      });
      
              AlertDialog dialog =builder.create();
              //show
              dialog.show();
          }
      

写在最后

想要学好安卓,不是到处看博客、看人家的文章、copy所谓的实现源码;而是去看官方文档。上面的代码全部都是直接从文档复制,简单处理的,可以直接用,文档有详细说明。加油!

Androi4.2文档下载

你可能感兴趣的:(android,对话框,自定义对话框,安卓文档)