我的第一章_对话框
- 普通对话框
- 日期对话框
- 时间对话框
- 进度条对话框
- 圆形对话框
- 自定义对话框
普通对话框
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
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 alertDialog = builder.create();
alertDialog.show();
日期对话框
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();
时间对话框
Calendar instance = Calendar.getInstance();
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();
}
},instance.get(Calendar.HOUR),instance.get(Calendar.MINUTE),true).show();
进度条对话框
final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setTitle("加载中");
progressDialog.setMessage("赚圈中");
progressDialog.setMax(100);
progressDialog.show();
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
int n=0;
@Override
public void run() {
n=n+25;
progressDialog.setProgress(n);
if(n==100){
timer.cancel();
progressDialog.dismiss();
}
}
},0,1000);
圆形对话框
与进度条一样,把stye改了就可以
自定义对话框
先建立一个layout.xml,里面是要求你的自定义布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/lay_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"></TextView>
<Button
android:id="@+id/lay_button"
android:text="按钮"
android:layout_width="match_parent"
android:layout_height="wrap_content"></Button>
<ImageView
android:id="@+id/lay_img"
android:src="@mipmap/ic_launcher"
android:layout_width="300dp"
android:layout_height="300dp"></ImageView>
</LinearLayout>
在创建一个bean类 里面继承Dialog类,里面set get方法让activity里面传值
package com.example.day1;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
public class Mylayput extends Dialog {
private TextView layText;
private Button layButton;
private ImageView layImg;
public String getLayTextstr() {
return layTextstr;
}
public void setLayTextstr(String layTextstr) {
this.layTextstr = layTextstr;
}
public String getLayButtonstr() {
return layButtonstr;
}
public void setLayButtonstr(String layButtonstr) {
this.layButtonstr = layButtonstr;
}
public String getLayImgstr() {
return layImgstr;
}
public void setLayImgstr(String layImgstr) {
this.layImgstr = layImgstr;
}
private String layTextstr;
private String layButtonstr;
private String layImgstr;
public Mylayput(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
layText = (TextView) findViewById(R.id.lay_text);
layButton = (Button) findViewById(R.id.lay_button);
layImg = (ImageView) findViewById(R.id.lay_img);
if(layTextstr!=null){
layText.setText(layTextstr);
}
if(layButtonstr!=null){
layButton.setText(layButtonstr);
}
layButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myOnclick.show();
}
});
}
public MyOnclick getMyOnclick() {
return myOnclick;
}
public void setMyOnclick(MyOnclick myOnclick) {
this.myOnclick = myOnclick;
}
MyOnclick myOnclick;
public interface MyOnclick{
void show();
}
}
Activity里面传值的操作
package com.example.day1;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TimePicker;
import android.widget.Toast;
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) {
Mylayput mylayput = new Mylayput(MainActivity.this);
mylayput.setLayTextstr("这是自定义文本语句");
mylayput.setLayButtonstr("这是自定义按钮的文本语句");
mylayput.setMyOnclick(new Mylayput.MyOnclick() {
@Override
public void show() {
Toast.makeText(MainActivity.this, "这是按钮的接口点击事件", Toast.LENGTH_SHORT).show();
}
});
mylayput.show();
}
}