俗话说 实践出真知 所以 就直接上代码啦
1、activity_main.xml
很基础 没什么好说的 顺着写就好
2、MainActivity.java
这里展示了5种弹窗(对话框) 的用法 但是大同小异
PS: 刚开始准备写在一起 但是太丑了 我都看不下去 还是分开展示吧 好气哦
0)主体
public class MainActivity extends AppCompatActivity {
private final Context context = MainActivity.this;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click1();
}
});
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click2();
}
});
button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click3();
}
});
button4 = findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click4();
}
});
button5 = findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
click5();
}
});
}
}
1)普通对话框
public void click1(){
//new一个建立弹窗模板的方法
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("基础弹窗");
builder.setMessage("弹窗是否显示成功?");
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "啊 这。。。", Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "弹窗显示成功", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
2)单选对话框
public void click2(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("请选择你最喜欢的选项");
final String items[] = {"A","B","C","D","E"};
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String item = items[which];
Toast.makeText(getApplicationContext(),"你选择了:" + item.toString(),Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
3)多选对话框
public void click3(){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("今天做了什么?");
final String items[] = {"吃饭","学习","睡觉","打豆豆"};
final boolean[] checkedItems = {false,true,false,false};
builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {}
});
builder.setPositiveButton("我选好了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < checkedItems.length; i++) {
if(checkedItems[i]){
String option = items[i];
sb.append(option + " ");
}
}
Toast.makeText(getApplicationContext(),"今天去 " + sb.toString() + "了",Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
4)加载进度条
public void click4(){
final ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("任务加载中...");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
new Thread(){ //创建一个子线程
public void run(){
dialog.setMax(100);
for (int i = 0; i <= 100; i++) {
dialog.setProgress(i);
SystemClock.sleep(50);
}
dialog.dismiss();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"加载成功",Toast.LENGTH_SHORT).show();
//可以同时设置多个view 或者发生消息(只允许运行在主线程的操作)
//安卓中的主线程叫UI线程 一个activity只允许有一个
}
});
}
}.start();
dialog.show();
}
5)自定义弹窗
public void click5(){
//new一个弹窗 并绑定布局
View view1 = View.inflate(context,R.layout.layout_one,null);
AlertDialog dialog = new AlertDialog.Builder(context)
.setView(view1)
.create();//只有这个方法的返回类型是 AlertDialog
TextView textView = view1.findViewById(R.id.text1);
//textView.setText("haha world!");
dialog.setCanceledOnTouchOutside(false);//触摸旁边不会消失,但返回键会消失
//dialog.setCancelable(false);//都不会消失
dialog.show();
}
3、layout_one.xml
这个就是写自定义弹窗的布局
PS: 鉴于本人水平太菜了 有些布局就强行加空格写的 哈哈哈哈偷懒了偷懒了
大概就这么多了吧 还是那句话 实践出真知! and 笔记都在注释里!