0x002 Dialog

0x000 环境:


  • 系统:Win10
  • IDE:Android Studio2.0

0x002 简单使用


  • 继承树:
    Dialog是AlertDialog、DatePickerDialog、ProgressDialog等一些常用Dialog的直接或者间接父类,但是官网文档说我们应当避免直接使用父类,而应该使用他们的子类。


    0x002 Dialog_第1张图片
    Paste_Image.png
  • AlertDialog最简单使用方式:

    • 代码:

      AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this);
              builder.setTitle("SimpleAlert");
              builder.setMessage("are you ok");
              builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      Toast.makeText(getApplicationContext(),"you click cancle",Toast.LENGTH_LONG).show();
      
                  }
              });
              builder.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      Toast.makeText(getApplicationContext(),"you click sure",Toast.LENGTH_LONG).show();
      
                  }
              });
              builder.create().show();
      
    • 效果:


      0x002 Dialog_第2张图片
      Paste_Image.png

    -说明:Dialog使用的是建造者模式,所以我们要用build来建造一个dialog,同时根据建造者模式的特性,我们还可以这么写:

        AlertDialog.Builder builder=new AlertDialog.Builder(DialogActivity.this);
              builder.setTitle("SimpleAlert").
              setMessage("are you ok").
              setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      Toast.makeText(getApplicationContext(),"you click cancle",Toast.LENGTH_LONG).show();
    
                  }
              }).
              setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      Toast.makeText(getApplicationContext(),"you click sure",Toast.LENGTH_LONG).show();
    
                  }
              }).create().show();
    
  • DialogFragment
    官网中推荐我们不应当这么使用,而是应该使用DialogFragment,因为这样可以更好地管理Dialog的生命周期和方法。所以接下来都会使用DialogFragment的方式来写

    • 代码:
      //MyDialogFragment.java
      public class MyDialogFragment extends android.support.v4.app.DialogFragment {
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
      builder.setTitle("first alert");
      builder.setMessage("are you OK");
      builder.setPositiveButton("Sure", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(getContext(),"you click sure",Toast.LENGTH_LONG).show();
      }
      });
      builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(getContext(),"you click cancle",Toast.LENGTH_LONG).show();

          }
      });
      return builder.create();
      }
      }
      //DialogActivity
      MyDialogFragment myDialogFragment=new MyDialogFragment();
      myDialogFragment.show(getSupportFragmentManager(),"");
      

0x003 AlertDialog


  • 再添加一个按钮
    • 代码:
      public class NatureButtonFragment extends DialogFragment {
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
      builder.setTitle("more button")
      .setMessage("more button")
      //确认按钮
      .setNegativeButton("SURE", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(getContext(),"you click sure",Toast.LENGTH_LONG).show();

                  }
              })
              //取消按钮
              .setPositiveButton("CANCLE", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      Toast.makeText(getContext(),"you click cancle",Toast.LENGTH_LONG).show();
      
                  }
              })
              //第三个按钮,不是确认也不是取消,而是介于二者中间,例如Remind Me Later
              .setNeutralButton("REMAIND ME LATER", new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      Toast.makeText(getContext(),"you click later",Toast.LENGTH_LONG).show();
                  }
              });
      return builder.create();
      
      }
      }
      
    • 效果

0x002 Dialog_第3张图片
Paste_Image.png
  • 添加一个列表
  • 代码:
    //ListDialogFragment.java
    public class ListDialogFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    final String s[] = new String[]{"item1", "item2", "item3"};
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("List")
    .setItems(s, new DialogInterface.OnClickListener() {
    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(getContext(), "you click " + s[which], Toast.LENGTH_LONG).show();
    }
    });
    return builder.create();
    }
    }
  • 效果:
0x002 Dialog_第4张图片
Paste_Image.png
  • 添加一列单选按钮
    • 代码:
      public class ListDialogFragment extends DialogFragment {
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
      final String s[] = new String[]{"item1", "item2", "item3"};
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
      builder.setTitle("List")
      .setItems(s, new DialogInterface.OnClickListener() {
      @TargetApi(Build.VERSION_CODES.M)
      @Override
      public void onClick(DialogInterface dialog, int which) {
      Toast.makeText(getContext(), "you click " + s[which], Toast.LENGTH_LONG).show();
      }
      });
      return builder.create();
      }
      }
    • 效果:
0x002 Dialog_第5张图片
Paste_Image.png
  • 添加一列多选按钮
    • 代码:
      public class MultipChoiceDialogFragment extends DialogFragment {
      @TargetApi(Build.VERSION_CODES.M)
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
      final String s[]={"item1","item2","item3"};
      final boolean s2[]={true,false,false};

                AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
                builder.setMultiChoiceItems(s, s2, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        Toast.makeText(getContext(),s[which]+" isChecked is "+isChecked,Toast.LENGTH_LONG).show();
                    }
                });
                return builder.create();
            }
        }
      
    -效果:
0x002 Dialog_第6张图片
Paste_Image.png
  • 自定义视图
    • 代码:
      //CustomerDialogFragment.java
      public class CustomerDialogFragment extends DialogFragment {
      @TargetApi(Build.VERSION_CODES.M)
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
      AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
      builder.setView(R.layout.fragment_dialog_customer);
      return builder.create();
      }
      }
      //R.layout.fragment_dialog_customer.xml

      android:orientation="vertical" android:layout_width="match_parent"
      android:padding="20dp"
      android:layout_height="match_parent">
      android:hint="username"
      android:inputType="textEmailAddress"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
      android:hint="password"
      android:inputType="textPassword"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      android:text="REGISTER"
      android:layout_weight="1"
      android:layout_width="0px"
      android:layout_height="wrap_content" />
      android:text="LOGIN"
      android:layout_weight="1"
      android:layout_width="0px"
      android:layout_height="wrap_content" />

    • 效果:
0x002 Dialog_第7张图片
Paste_Image.png
  • 在activity中处理按钮事件:
    • 代码:
      //EventBackDialogFragment .java
      public class EventBackDialogFragment extends DialogFragment {
      public interface EventBackDialogListener{
      public void OnPositiveClick(DialogFragment dialogFragment);
      public void OnNegativeClick(DialogFragment dialogFragment);
      }
      EventBackDialogListener mListener;
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {

                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mListener.OnNegativeClick(EventBackDialogFragment.this);
                    }
                })
                        .setPositiveButton("SURE", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mListener.OnPositiveClick(EventBackDialogFragment.this);
                            }
                        });
                return builder.create();
        
            }
        
            @Override
            public void onAttach(Activity activity) {
        
                super.onAttach(activity);
                mListener= (EventBackDialogListener) activity;
            }
        }
        //DialogActivity
      public class DialogActivity extends ListActivity implements   EventBackDialogFragment.EventBackDialogListener {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          EventBackDialogFragment eventBackDialogFragment=new EventBackDialogFragment();
            eventBackDialogFragment.show(getFragmentManager(),"");
        }
       @Override
      public void OnPositiveClick(android.app.DialogFragment dialogFragment) {
      Toast.makeText(getApplicationContext(),"you  click Sure",Toast.LENGTH_LONG).show();
      
      }
      
      @Override
      public void OnNegativeClick(android.app.DialogFragment dialogFragment) {
       Toast.makeText(getApplicationContext(),"you  click Cancle",Toast.LENGTH_LONG).show();
      
      }
      }
      

0x004 ProgressDialog


  • 概述:progressDialog和DatapickerDilaog等其实不过是AlertDialog的子类,所以说白了,不过是一个封装好的Dialog,比如progressDlialog不过是封装了一个progress的AlertDialog。
0x002 Dialog_第8张图片
Paste_Image.png
  • progressDialog进度条
    • 代码:
      public class ProgressDialogFragment extends DialogFragment {
      @TargetApi(Build.VERSION_CODES.M)
      @NonNull
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {

              ProgressDialog progressDialog=new ProgressDialog(getContext());
              return progressDialog;
          }
      }
      
    • 效果图

0x002 Dialog_第9张图片
Paste_Image.png
  • 带进度的progress
    在ProgressDialog.java中,有一个方法名为setProgressStyle(int style ),我们跳到mProgressStyle,就会看到
    public static final int STYLE_SPINNER = 0;//spinner进度条
    public static final int STYLE_HORIZONTAL = 1;//horizontal进度条
    0x002 Dialog_第10张图片
    Paste_Image.png
0x002 Dialog_第11张图片
Paste_Image.png
  • 代码:

     public class ProgressHorizatalDialogFragment extends DialogFragment {
       @TargetApi(Build.VERSION_CODES.M)
       @NonNull
       @Override
       public Dialog onCreateDialog(Bundle savedInstanceState) {
    
           ProgressDialog progressDialog=new ProgressDialog(getContext());
           progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
           progressDialog.setMessage("正在下载");
           progressDialog.setProgress(20);
           progressDialog.setMax(100);
           return progressDialog;
       }
     }
    
  • 效果

0x002 Dialog_第12张图片
Paste_Image.png

0x005 DatePickerDialog


  • 代码:
    public class DatePickerDialogFragment extends DialogFragment {
    @TargetApi(Build.VERSION_CODES.M)
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    DatePickerDialog datePickerDialog=new DatePickerDialog(getContext(), new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                  }
              },2016,10,12);
              return datePickerDialog;
      
          }
      }
    
  • 效果:

0x002 Dialog_第13张图片
Paste_Image.png

TimePickerDialog

  • 代码
    public class TimePickerDialogFragment extends DialogFragment {
    @TargetApi(Build.VERSION_CODES.M)
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
    TimePickerDialog timePickerDialog=new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Toast.makeText(getContext(),"you choice"+hourOfDay+":"+minute,Toast.LENGTH_LONG).show();
    }
    },12,12,true);
    timePickerDialog.show();
    return timePickerDialog;
    }
    }
  • 效果
0x002 Dialog_第14张图片
Paste_Image.png

你可能感兴趣的:(0x002 Dialog)