Android Studio八种对话框

@Android Studio八种对话框

布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >
   
   <Button
       android:onClick="onclick"
       android:layout_weight="1"
       android:id="@+id/bt1"
       android:text="普通对话框"
       android:layout_width="match_parent"
       android:layout_height="match_parent"></Button>
    <Button
        android:id="@+id/bt2"
        android:onClick="onclick"
        android:text="单选对话框"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></Button>
    <Button
        android:text="多选对话框1"
        android:id="@+id/bt3"
        android:onClick="onclick"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></Button>
    <Button
        android:text="多选对话框2"
        android:id="@+id/bt4"
        android:onClick="onclick"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></Button>
    <Button
        android:text="水平进度条对话框对话框"
        android:id="@+id/bt5"
        android:onClick="onclick"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></Button>
    <Button
        android:text="圆形进度条对话框"
        android:id="@+id/bt6"
        android:onClick="onclick"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></Button>
    <Button
        android:text="日期选择对话框"
        android:id="@+id/bt7"
        android:onClick="onclick"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></Button>
    <Button
        android:text="时间选择对话框"
        android:id="@+id/bt8"
        android:onClick="onclick"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></Button>
 <Button
     android:text="简单自定义对话框"
     android:id="@+id/bt9"
     android:onClick="onclick"
     android:layout_weight="1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"></Button>
 .
</LinearLayout>
java文件
package com.example.myapplication2;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ProgressBar;
import android.widget.TimePicker;
import android.widget.Toast;

import java.sql.Time;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


    }



    public void onclick(View view) {
    
    
       switch (view.getId()){
    
    
           case R.id.bt1:
               setbt1();
               break;
           case R.id.bt2:
               setbt2();
               break;
           case R.id.bt3:
               setbt3();
               break;
           case R.id.bt4:
               setbt4();
               break;
           case R.id.bt5:
               setbt5();
               break;
           case R.id.bt6:
               setbt6();
               break;
           case R.id.bt7:
               setbt7();
               break;
           case R.id.bt8:
               setbt8();
               break;
           case R.id.bt9:
               setbt9();
               break;
       }


    }

    private void setbt9() {
    
    

    }

    private void setbt8() {
    
    
        Calendar calendar=Calendar.getInstance();
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
    
    
           @Override
           public void onTimeSet(TimePicker timePicker, int i, int i1) {
    
    
               Toast.makeText(MainActivity.this, i+":"+i1,                          Toast.LENGTH_SHORT).show();
           }
       },calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
    }
    private void setbt7() {
    
    
        Calendar calendar=Calendar.getInstance();
     new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
    
    
         @Override
         public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
    
    
             Toast.makeText(MainActivity.this, i+"/"+(i1+1)+"/"+i2, Toast.LENGTH_SHORT).show();
         }
     },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    private void setbt6() {
    
    
        final ProgressDialog dialog=new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMax(100);
        dialog.setMessage("下载中");
        dialog.show();
        final Timer timer=new Timer();
        timer.schedule(new TimerTask() {
    
    
            int pro=0;
            @Override
            public void run() {
    
    
                if(pro==100){
    
    
                    dialog.dismiss();
                    timer.cancel();
                }
                dialog.setProgress(pro+=10);
            }
        },0,1000);
    }

    private void setbt5() {
    
    
       final ProgressDialog dialog=new ProgressDialog(this);
       dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
         dialog.setMax(100);
         dialog.setMessage("下载中");
         dialog.show();
         final Timer timer=new Timer();
         timer.schedule(new TimerTask() {
    
    
             int pro=0;
             @Override
             public void run() {
    
    
                 if(pro==100){
    
    
                     dialog.dismiss();
                     timer.cancel();
                 }
                 dialog.setProgress(pro+=10);
             }
         },0,1000);
    }

    private void setbt4() {
    
    
       final   String[] items={
    
    "萝莉","御姐","正太","少女","女装大佬","萌妹","软妹子","JK","ACG","二次元"};
        final boolean[] flags={
    
    true,true,true,true,true,true,true,true,true,false};
        AlertDialog.Builder al=new AlertDialog.Builder(this);
        al.setIcon(R.drawable.ic_launcher_foreground);
        al.setTitle("他的女朋友们");
        al.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    
    
            @Override
            public void onClick(DialogInterface dialogInterface, int ii) {
    
    
             for (int i=0;i<flags.length;i++){
    
    
                 if(flags[i]){
    
    
                     Toast.makeText(MainActivity.this, "你选择的是"+items[i], Toast.LENGTH_SHORT).show();
                 }
             }
            }
        });
        al.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    
    
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
    
    
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        al.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
    
    
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b) {
    
    
                if(flags[i]==true){
    
    
                    Toast.makeText(MainActivity.this, "你选择的是"+items[i], Toast.LENGTH_SHORT).show();
                }else {
    
    
                    flags[i]=b;
                }

            }
        });
        AlertDialog log=al.create();
        log.show();

    }

    private void setbt3() {
    
    
   AlertDialog log=new AlertDialog.Builder(this).setIcon(R.mipmap.ic_launcher)
    .setPositiveButton("选好啦",null)
           .setNegativeButton("下次再说吧",null)
           .setMultiChoiceItems(new String[]{
    
    "萝莉","御姐","正太","少女","女装大佬","萌妹","软妹子","JK","ACG","二次元"},null,null).create();
   log.show();
    }

    private void setbt2() {
    
    
         AlertDialog.Builder builder=new AlertDialog.Builder(this);
         builder.setIcon(R.drawable.ic_launcher_foreground);
         builder.setTitle("中国的地方");
         builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    
    
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
    
    
                 Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();
             }
         });
         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    
    
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
    
    
                 Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
             }
         });
         final String[] s={
    
    "北京","上海","天津","安徽"};
         builder.setSingleChoiceItems(s, 2, new DialogInterface.OnClickListener() {
    
    
             @Override
             public void onClick(DialogInterface dialogInterface, int i) {
    
    
                 Toast.makeText(MainActivity.this, "你选择的是"+s[i], Toast.LENGTH_SHORT).show();
             }
         });
          AlertDialog log=builder.create();
          log.show();
    }

    private void setbt1() {
    
    
        AlertDialog.Builder ab=new AlertDialog.Builder(this);
        ab.setIcon(R.drawable.ic_launcher_background);
        ab.setTitle("这个是普通对话框");
        ab.setMessage("我是罗维");
        ab.setPositiveButton("确定", new DialogInterface.OnClickListener() {
    
    
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
    
    
                Toast.makeText(MainActivity.this, "你点击了确定", Toast.LENGTH_SHORT).show();
            }
        });
        ab.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    
    
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
    
    
                Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
            }
        });
        AlertDialog log=ab.create();
        log.show();

    }
}

自定义复杂
创建小布局的对象
package com.example.myapplication4;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class DiyDialog extends Dialog {
    
    
 private Button bt1,bt2;
 private  ImageView ig;
 private  TextView tv1,tv2;
    private String strTitle1,strmessage2;

    public String getStrTitle1() {
    
    
        return strTitle1;
    }

    public void setStrTitle1(String strTitle1) {
    
    
        this.strTitle1 = strTitle1;
    }

    public String getStrmessage2() {
    
    
        return strmessage2;
    }

    public void setStrmessage2(String strmessage2) {
    
    
        this.strmessage2 = strmessage2;
    }

    public TextView getTv1() {
    
    
        return tv1;
    }

    public TextView getTv2() {
    
    
        return tv2;
    }

    public void setTv1(TextView tv1) {
    
    
        this.tv1 = tv1;
    }

    public void setTv2(TextView tv2) {
    
    
        this.tv2 = tv2;
    }

    public DiyDialog(@NonNull Context context) {
    
    
        super(context);
    }

     public  interface   Igclick{
    
    
        void  igclick();
     }
     private  Igclick igclick;

    public Igclick getIgclick() {
    
    
        return igclick;
    }

    public void setIgclick(Igclick igclick) {
    
    
        this.igclick = igclick;
    }

    public  interface  Yesook{
    
    
        void yesclick();
    }
    private  Yesook yesook;

    public Yesook getYesook() {
    
    
        return yesook;
    }

    public void setYesook(Yesook yesook) {
    
    
        this.yesook = yesook;
    }

    public interface  Nonoook{
    
    
        void notext();
    }
    private Nonoook nonoooks;

    public Nonoook getNonoooks() {
    
    
        return nonoooks;
    }

    public void setNonoooks(Nonoook nonoooks) {
    
    
        this.nonoooks = nonoooks;
    }

    public DiyDialog(@NonNull Context context, int themeResId) {
    
    
        super(context, themeResId);
    }

    protected DiyDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
    
    
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item);
        inidtext();
        if(strTitle1!=null){
    
    
            tv1.setText(strTitle1);
        }
        if(strmessage2!=null){
    
    
            tv2.setText(strmessage2);
        }
      bt1.setOnClickListener(new View.OnClickListener() {
    
    
          @Override
          public void onClick(View view) {
    
    
              yesook.yesclick();
              dismiss();
          }
      });
        bt2.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
              nonoooks.notext();
              dismiss();
            }
        });
        ig.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                igclick.igclick();
                dismiss();
            }
        });

    }
    private void inidtext() {
    
    
    bt1=findViewById(R.id.bt1);
    bt2=findViewById(R.id.bt2);
    tv1=findViewById(R.id.tv1);
    tv2=findViewById(R.id.tv2);
        ig=findViewById(R.id.ig);
    }



}

调用上面的类
package com.example.myapplication4;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    
    
private Button bt11;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onclick(View view) {
    
    
        switch (view.getId()){
    
    
            case  R.id.bt11:
                intextbt1();
                break;
        }
    }
    private void intextbt1() {
    
    
   
        DiyDialog diyDialog=new DiyDialog(this);
      diyDialog.setStrTitle1("asdasd");
      diyDialog.setStrmessage2("adasdasdasdasasdas");
    diyDialog.setYesook(new DiyDialog.Yesook() {
    
    
        @Override
        public void yesclick() {
    
    
            Toast.makeText(MainActivity.this, "123", Toast.LENGTH_SHORT).show();
        }
    });
    diyDialog.setNonoooks(new DiyDialog.Nonoook() {
    
    
        @Override
        public void notext() {
    
    
            Toast.makeText(MainActivity.this, "456", Toast.LENGTH_SHORT).show();
        }
    });
    diyDialog.setIgclick(new DiyDialog.Igclick() {
    
    
        @Override
        public void igclick() {
    
    
            Toast.makeText(MainActivity.this, "牛逼", Toast.LENGTH_SHORT).show();
        }
    });
      diyDialog.show();
    }
}

主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

     <LinearLayout
         android:layout_weight="10"
         android:layout_width="match_parent"
         android:layout_height="match_parent"></LinearLayout>
    <Button
        android:onClick="onclick"
        android:id="@+id/bt11"
        android:text="评一评"
        android:textSize="25sp"
        android:textColor="#ffffff"
        android:layout_weight="1"
        android:background="@drawable/tp"
        android:layout_width="match_parent"
        android:layout_height="100dp"></Button>

</LinearLayout>
item小布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="300dp"
    android:layout_height="300dp"
    >
     <LinearLayout
         android:orientation="vertical"
         android:layout_weight="1"
         android:background="#2196F3"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
         <ImageView
             android:id="@+id/ig"
             android:layout_weight="3"
             android:src="@mipmap/aa"
             android:layout_marginLeft="280dp"
             android:layout_width="match_parent"
             android:layout_height="match_parent"></ImageView>
         <TextView
             android:id="@+id/tv1"
             android:gravity="center|top"
             android:text="评价一下吧!"
             android:layout_weight="1"
             android:textColor="#ffffff"
             android:textSize="25sp"
             android:layout_width="match_parent"
             android:layout_height="match_parent"></TextView>
     </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv2"
            android:gravity="center|top"
            android:text="喜欢吗?给一个5星好评,鼓励一下我们做的更好"
            android:layout_weight="1"
            android:textSize="19sp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></TextView>

    </LinearLayout>
    <LinearLayout
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/bt1"
            android:background="#ffffff"
            android:layout_weight="1"
            android:text="我要吐槽"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="80dp"></Button>
        <Button
            android:id="@+id/bt2"
            android:textColor="#2196F3"
            android:text="五星好评"
            android:gravity="center"
            android:background="#ffffff"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="80dp"></Button>
    </LinearLayout>
</LinearLayout>

猜你喜欢

转载自blog.csdn.net/mynameisluowei/article/details/108516406
今日推荐