第一天:Dialog对话框
一:常用的对话框
1.普通对话框
2.单选对话框
3.多选对话框
4.自定义对话框
5.水平进度条对话框
6.圆形进度条对话框
7.日期选择对话框
8.时间选择对话框
二:自定义对话框
1.思路
2.代码
1.普通对话框
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("你是一个傻子");
builder.setTitle("1706");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
};
});
2.单选对话框
private void single_dialog() {
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("点我就完事");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了ok",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
}
});
final String[] items={"男人","女人","人妖"};
builder.setSingleChoiceItems(items, 2, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你是一个傻子"+items[which], Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
3.多选对话框
private void mulite_dialog() {
final String[] items={"1","2","3","4"};
final boolean[] flags={true,true,true,false};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("尚鸿昆的女朋友们");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for(int i=0;i<flags.length;i++){
if(flags[i]){
Toast.makeText(MainActivity.this,"是"+items[i],Toast.LENGTH_SHORT).show();
}
}
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"你点击了cancel",Toast.LENGTH_SHORT).show();
}
});
builder.setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
flags[which]=isChecked;
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
4.自定义对话框
View inflate = LayoutInflater.from(this).inflate(R.layout.my_layout, null);
View viewById = inflate.findViewById(R.id.iv);
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(inflate);
AlertDialog alertDialog = builder.create();
alertDialog.show();
5.水平进度条对话框
builder.setView(inflate);
AlertDialog alertDialog = builder.create();
alertDialog.show();
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.setMessage("正在缓存");
progressDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int progress = 0;
@Override
public void run() {
if (progress == 100){
progressDialog.dismiss();
timer.cancel();
}
progressDialog.setProgress(progress+=20);
}
},0,1000);
7.日期选择对话框
Calendar instance = Calendar.getInstance();
new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"-"+(month-1)+dayOfMonth, Toast.LENGTH_SHORT).show();
}
},instance.get(Calendar.YEAR),instance.get(Calendar.MONTH),instance.get(Calendar.DAY_OF_MONTH)).show();
8.时间选择对话框
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
}
},11,12,true);
二:自定义对话框
package com.example.day01;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog<onNoOnclickListener> extends Dialog {
private TextView text1;
private TextView text2;
private Button buttonYes;
private Button buttonNo;
private String titleStr;
private String messageStr;
private String yesStr,noStr;
private onNoOnclickListener onNoOnclickListener;
public MyDialog( Context context, int themeResId) {
super(context, themeResId);
WindowManager windowManager = getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.width = display.getWidth()*4/5;
getWindow().setAttributes(attributes);
}
public MyDialog( Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
public void setOnNoOnclickListener(String str,onNoOnclickListener onNoOnclickListener){
if (str != null){
noStr = str;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myself_layout);
setCanceledOnTouchOutside(false);
initView();
initDate();
initEvent();
}
private void initEvent() {
buttonYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (onNoOnclickListener!= null){
}
}
});
}
public void setText1(String text1){
titleStr = text1;
}
public void setText2(String text2){
messageStr = text2;
}
private void initDate() {
if (titleStr != null){
text1.setText(titleStr);
}
if (messageStr != null){
text2.setText(messageStr);
}
if (yesStr != null){
buttonYes.setText(yesStr);
}
if (noStr!= null){
buttonNo.setText(noStr);
}
}
private void initView() {
buttonYes = findViewById(R.id.button_yes);
buttonNo = findViewById(R.id.button_no);
text1 = findViewById(R.id.text_1);
text2 = findViewById(R.id.text_2);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
>
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="警告"
android:textSize="30sp"
android:layout_marginTop="10dp"
android:textColor="#f0f"
/>
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center"
android:text="少玩手机,保护视力"
android:textSize="20sp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="15dp"
></View>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:orientation="horizontal"
>
<Button
android:id="@+id/button_yes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="70dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="取消"
android:textSize="20sp"
/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#ff0"></View>
<Button
android:id="@+id/button_no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="40dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:text="确定"
android:textSize="20sp"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>