1705A霍思远----Day01
欢迎浏览本博客—DIALOG
1.普通对话框`
public void normal_dialog(){
//TODO 1:构建者
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//TODO 2:设置属性
builder.setIcon(R.drawable.ic_launcher_background);//设置图标
builder.setTitle("God is a Girl");//设置标题
builder.setMessage("Do you think God is girl?");//设置内容
//设置确定按钮
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();
}
});
//TODO 3:使用建造者创建对话框
AlertDialog dialog = builder.create();
//TODO 4:显示
dialog.show();
}
2.单选对话框
public void single_button(){
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, "你點擊了確定", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你點擊了取消", 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={"羅玉鳳","王翠花","李酸菜","趙鐵柱"};
final boolean[] flags={true,true,false,false};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher_background);
builder.setTitle("請選擇張萌的小寶貝:");
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; 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, "這顯然不是個明智之舉", 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.自定义对话框
private void DIY_dialog() {
View view= LayoutInflater.from(this).inflate(R.layout.activity_main2,null);
ImageView imageView=view.findViewById(R.id.iv);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你點擊了確定", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "你點擊了取消", Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
5.水平进度条
private void hor_progress_dialog() {
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);
}
6.圆形进度条
private void spi_progress_dialog() {
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
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.日期选择对话框
private void date_dialog() {
Calendar calendar=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();
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
8.时间选择对话框
private void time_dialog() {
Calendar calendar=Calendar.getInstance();//日历对象
//TODO 1:实例化对象 TODO 2:show
new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();
}
},calendar.get(Calendar.HOUR),calendar.get(Calendar.MINUTE),true).show();
}
自定義對話框(效果圖)
代碼
佈局文件:
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:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:background="@mipmap/aa"
android:orientation="vertical">
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:textColor="#FFEB3B"
android:textSize="16sp"/>
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_gravity="center" />
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="15dp"
android:background="#FFEB3B"/>
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
android:id="@+id/no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:textColor="#E91E63"
android:textSize="16sp"/>
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#E91E63"/>
android:id="@+id/yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="@null"
android:gravity="center"
android:lines="1"
android:textColor="#FFEB3B"
android:textSize="16sp"/>
DIYDialog.java:
package com.example.myapplication;
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 DiyDialog extends Dialog {
private TextView title;
private TextView message;
private Button no;
private Button yes;
private String titleStr;//从外界设置的title文本
private String messageStr;//从外界设置的消息文本
private String yesStr, noStr;
private onNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
private onYesOnclickListener yesOnclickListener;
public DiyDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
public DiyDialog( Context context, int themeResId) {
super(context, themeResId);
WindowManager windowManager = getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = display.getWidth()*4/5;
getWindow().setAttributes(lp);
}
public void setNoOnclickListener(String str,onNoOnclickListener noOnclickListener) {
if (str!=null){
noStr=str;
}
this.noOnclickListener = noOnclickListener;
}
public void setYesOnclickListener(String str,onYesOnclickListener yesOnclickListener) {
if (str!=null){
yesStr=str;
}
this.yesOnclickListener = yesOnclickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setCanceledOnTouchOutside(false);
initView();
initData();
initEvent();
}
private void initEvent() {
yes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (yesOnclickListener!=null){
yesOnclickListener.onYesclick();
}
}
});
no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (noOnclickListener!=null){
noOnclickListener.onNOclick();
}
}
});
}
private void initData() {
if (titleStr!=null){
title.setText(titleStr);
}
if (messageStr!=null){
message.setText(messageStr);
}
if (yesStr!=null){
yes.setText(yesStr);
}
if (noStr!=null){
no.setText(noStr);
}
}
private void initView() {
title = (TextView) findViewById(R.id.title);
message = (TextView) findViewById(R.id.message);
no = (Button) findViewById(R.id.no);
yes = (Button) findViewById(R.id.yes);
}
public void setTitleStr(String titleStr) {
this.titleStr = titleStr;
}
public void setMessageStr(String messageStr) {
this.messageStr = messageStr;
}
public interface onNoOnclickListener{
public void onNOclick();
}
public interface onYesOnclickListener{
public void onYesclick();
}
}
Main方法中賦值:
package com.example.myapplication;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DiyDialog diyDialog = new DiyDialog(this);
diyDialog.setTitle("你喜歡這個小姐姐嗎?");
diyDialog.setMessageStr("我知道你愛的不得了");
diyDialog.setYesOnclickListener("是是是", new DiyDialog.onYesOnclickListener() {
@Override
public void onYesclick() {
Toast.makeText(MainActivity.this, "你点了q", Toast.LENGTH_SHORT).show();
}
});
diyDialog.setNoOnclickListener("不不不", new DiyDialog.onNoOnclickListener() {
@Override
public void onNOclick() {
Toast.makeText(MainActivity.this, "你点了c", Toast.LENGTH_SHORT).show();
}
});
diyDialog.show();
}
}
标签:Toast,builder,Day01,public,import,new,void
来源: https://blog.csdn.net/weixin_45365635/article/details/94761932