zoukankan      html  css  js  c++  java
  • Android之AlterDialog介绍

    效果:

     

    1.简单对话框

    AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            builder
                    .setTitle("对话框")
                    .setIcon(R.mipmap.ic_launcher_round)
                    .setPositiveButton("确定",null)
                    .setNegativeButton("取消",null)
                    .setMessage("简单对话框")
                    .create()
                    .show();
    

      

    2.列表对话框

            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            final String[] mItems={"item0","item1","item2","item3","item4","item5","item6"};
            builder.setTitle("列表对话框")
                    .setItems(mItems, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(AlterDialogActivity.this,mItems[i],Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setPositiveButton("确定",null)
                    .setNegativeButton("取消",null)
                    .create()
                    .show();
    

      

    3.单选对话框

            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            final String[] items={"item1","item2","item3","item4"};
            builder.setTitle("单选对话框")
                    .setIcon(R.mipmap.ic_launcher_round)
                    .setSingleChoiceItems(items,0,new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(AlterDialogActivity.this,items[i]+"被点击了",Toast.LENGTH_SHORT).show();
                            singleChoise=i;
                        }
                    })
                    .setPositiveButton("确定",null)
                    .setNegativeButton("取消",null)
                    .create()
                    .show();
    

      

    4.多选对话框

            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            final String[] items={"item1","item2","item3","item4"};
            builder.setTitle("多选对话框")
                    .setIcon(R.mipmap.ic_launcher_round)
                    .setMultiChoiceItems(items,new boolean[]{false,false,false,false},new DialogInterface.OnMultiChoiceClickListener(){
    
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                            if(b){
                                Toast.makeText(AlterDialogActivity.this,items[i]+"被选中了",Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(AlterDialogActivity.this,items[i]+"被取消选中",Toast.LENGTH_SHORT).show();
                            }
                        }
                    })
                    .setPositiveButton("确定",null)
                    .setNegativeButton("取消",null)
                    .create()
                    .show();
    

      

    5.自定义View对话框

            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            View view= LayoutInflater.from(this).inflate(R.layout.alterdialot_img,null);
            builder
    //                .setTitle("自定义对话框")
    //                .setIcon(R.mipmap.ic_launcher_round)
                    .setView(view)
                    .setCancelable(true)
                    .setNegativeButton("取消",null)
                    .create()
                    .show();
    

      

     代码:

    xml:

    <?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:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="对话框" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="列表对话框" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="单选对话框" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="多选对话框" />
    
        <Button
            android:id="@+id/button5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="click"
            android:text="自定义对话框" />
    </LinearLayout>
    

      activity

    public class AlterDialogActivity extends AppCompatActivity {
    //    private AlertDialog.Builder builder;
        private int singleChoise=0;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.act_alterlog);
    //        builder=new AlertDialog.Builder(this);
        }
    
        public void click(View v){
            switch (v.getId()){
                case R.id.button://简单对话框
                    showDialog1();
                    break;
                case R.id.button2://列表框
                    showDialog2();
                    break;
                case R.id.button3://单选框
                    showDialog3();
                    break;
                case R.id.button4://多选框
                    showDialog4();
                    break;
                case R.id.button5://自定义contentview
                    showDialog5();
                    break;
            }
        }
    
        private void showDialog1() {
            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            builder
                    .setTitle("对话框")
                    .setIcon(R.mipmap.ic_launcher_round)
                    .setPositiveButton("确定",null)
                    .setNegativeButton("取消",null)
                    .setMessage("简单对话框")
                    .create()
                    .show();
        }
        private void showDialog2() {
            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            final String[] mItems={"item0","item1","item2","item3","item4","item5","item6"};
            builder.setTitle("列表对话框")
                    .setItems(mItems, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(AlterDialogActivity.this,mItems[i],Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setPositiveButton("确定",null)
                    .setNegativeButton("取消",null)
                    .create()
                    .show();
    
    
    
        }
        private void showDialog3() {
            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            final String[] items={"item1","item2","item3","item4"};
            builder.setTitle("单选对话框")
                    .setIcon(R.mipmap.ic_launcher_round)
                    .setSingleChoiceItems(items,0,new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(AlterDialogActivity.this,items[i]+"被点击了",Toast.LENGTH_SHORT).show();
                            singleChoise=i;
                        }
                    })
                    .setPositiveButton("确定",null)
                    .setNegativeButton("取消",null)
                    .create()
                    .show();
    
    
        }
        private void showDialog4() {
            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            final String[] items={"item1","item2","item3","item4"};
            builder.setTitle("多选对话框")
                    .setIcon(R.mipmap.ic_launcher_round)
                    .setMultiChoiceItems(items,new boolean[]{false,false,false,false},new DialogInterface.OnMultiChoiceClickListener(){
    
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                            if(b){
                                Toast.makeText(AlterDialogActivity.this,items[i]+"被选中了",Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(AlterDialogActivity.this,items[i]+"被取消选中",Toast.LENGTH_SHORT).show();
                            }
                        }
                    })
                    .setPositiveButton("确定",null)
                    .setNegativeButton("取消",null)
                    .create()
                    .show();
    
        }
        private void showDialog5() {
            AlertDialog.Builder builder=new AlertDialog.Builder(AlterDialogActivity.this);
            View view= LayoutInflater.from(this).inflate(R.layout.alterdialot_img,null);
            builder
    //                .setTitle("自定义对话框")
    //                .setIcon(R.mipmap.ic_launcher_round)
                    .setView(view)
                    .setCancelable(true)
                    .setNegativeButton("取消",null)
                    .create()
                    .show();
    
        }
    
    }
    

      

  • 相关阅读:
    Beta阶段冲刺---Day1
    网络15软工个人作业5——软件工程总结
    软工网络15个人作业4——alpha阶段个人总结
    软件工程个人作业3——集大通APP案例分析
    软件工程网络15结对编程作业
    软件工程网络15个人阅读作业2-提出问题
    软工网络15个人阅读作业1
    Java课程设计—学生成绩管理系统(201521123002 林楚虹)
    201521123002《Java程序设计》第14周学习总结
    201521123002 《Java程序设计》第13周学习总结
  • 原文地址:https://www.cnblogs.com/wangjiaghe/p/7131755.html
Copyright © 2011-2022 走看看