Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)

Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)

1.普通对话框

2.单选对话框

3.多选对话框

4.进度条对话框

5.消息对话框

6.自定义对话框

1.普通对话框

//1.创建普通对话框
package com.krisantisce.androiddialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("dialog对话框") //设置标题
.setMessage("是否确定退出?") //设置提醒的信息
.setIcon(R.drawable.log) //设置图标
.setPositiveButton("确定",null) //添加确定按钮
.setNegativeButton("取消",null) //添加取消按钮
.create();
dialog.show();
}
}

效果:

Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)


2.单选对话框

//2.创建单选对话框
package com.krisantisce.androiddialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog dialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.log) //添加图标
.setTitle("请选择你的性别^_^")
.setPositiveButton("确定", null) //添加确定按钮
.setNegativeButton("取消", null) //添加取消按钮
.setSingleChoiceItems(new String[]{"男", "女"}, -1, //负数表示没有默认选择;0默认选择"男",1默认选择"女"
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) { }
})
.create();
dialog.show();
}
}

效果:

Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)


3.多选对话框

//3.创建多选对话框
package com.krisantisce.androiddialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setIcon(R.drawable.log) //设置图标
.setTitle("请选择你感兴趣的类型^_^") //设置标题
.setPositiveButton("选好啦",null) //设置确定按钮
.setNegativeButton("下次再说吧",null) //设置取消按钮
.setMultiChoiceItems(new String[]{"萝莉","御姐","正太","少女","女装大佬","萌妹","软妹子","JK","ACG","二次元"},null,null)
.create();
alertDialog.show();
}
}

效果:

Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)


4.进度条对话框

// 4.创建进度条对话框
package com.krisantisce.androiddialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ProgressDialog progressDialog = new ProgressDialog(this); //构建对话框
progressDialog.setTitle("进度条对话框");
progressDialog.setIcon(R.drawable.log);
progressDialog.setMessage("正在下载,请稍后^_^");
//设置水平进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //条形进度条
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //圆形进度条
progressDialog.show();
}
}

效果:

Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)

Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)


5.消息对话框

//5.消息对话框
package com.krisantisce.androiddialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this,"HrlloWorld",Toast.LENGTH_SHORT).show(); //通知时间短
Toast.makeText(this,"HelloWorld",Toast.LENGTH_LONG).show(); //通知时间长
}
}

效果:

Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)


6.自定义对话框

  • 创建自定义对话框样式,这里以mydialog.xml为例;
  • 创建MyDialog类;
  • MainActivity调用MyDialog类

创建mydialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="提示"
android:background="#E619E3"
android:textSize="28sp"/>
<TextView
android:id="@+id/tv_2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="你真的要离开吗"
android:textColor="#ff0400"
android:background="@drawable/log3"
android:textSize="28sp"/>
<LinearLayout
android:id="@+id/ll_1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50sp"
android:gravity="center"
android:background="#D3DF26">
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="残忍离开"
android:textSize="24sp"
android:background="#06F18B"
android:layout_marginRight="30dp"
android:onClick="okclick"/>
<Button
android:id="@+id/btn_2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="28sp"
android:text="马上进入"
android:background="#8284E1"
android:layout_marginLeft="30dp"
android:onClick="canclick"/>
</LinearLayout> </LinearLayout>

创建MyDialog类

package com.krisantisce.androiddialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull; public class MyDialog extends Dialog { //继承父类
private String dialogName;
public MyDialog(@NonNull Context context, String dialogName) {
super(context);
this.dialogName = dialogName; } @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); // 去除标题
setContentView(R.layout.mydialog); // 引入自定义对话框布局
TextView textView = findViewById(R.id.tv_2);
Button button_no = findViewById(R.id.btn_1);
Button button_ok = findViewById(R.id.btn_2);
//设置自定义对话框显示内容
textView.setText(dialogName);
//给按钮设置点击事件
button_ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { }
}); button_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 把对话框关闭
dismiss();
}
});
}
}

MainActivity调用MyDialog类

package com.krisantisce.androiddialog;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); MyDialog myDialog = new MyDialog(this,null);
myDialog.show(); }
}

效果:

Android Studio常见对话框(普通对话框、单选对话框、多选对话框、进度条对话框、消息对话框、自定义对话框)

上一篇:IOS贝塞尔曲线圆形进度条和加载动画


下一篇:WMS二开:外挂页面开发培训