AlertDialog
- AlertDialog可以在当前的界面弹出一个对话框,这个对话框置顶于所有的界面之上,能够屏蔽掉其他控件的的交互能力,因此,AlertDialog一般都是用来提示一些比较重要内容或者警告,下面来看下它的用法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("对话框");
dialog.setMessage("你好!");
dialog.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
}
});
}
}
- 在Button的点击事件中,我们通过AlertDialog.Builder创建了一个AlertDialog实例,然后为这个对话框设置了标题,内容,以及点击事件
ProgressDialog
- ProgressDialog与AlertDialog有点类似,都可以在界面上弹出一个对话框,不过ProgressDialog不同的是,它会在对话框中显示一个进度条
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("对话框");
progressDialog.setMessage("你好!");
progressDialog.setCancelable(true);
progressDialog.show();
}
});
}
}
自定义对话框
- 在styles文件下添加如下代码,设置属性
<style name="NormalDialogStyle">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">false</item>
</style>
- 另外创建一个layout,命名为dialog_normal,用于实现对话框的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:text="1111"
android:textSize="20sp" />
</LinearLayout>
- Button中的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(MainActivity.this, R.style.NormalDialogStyle);
View view = View.inflate(MainActivity.this, R.layout.dialog_normal, null);
dialog.setContentView(view);
dialog.setCanceledOnTouchOutside(true);
Window dialogWindow = dialog.getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
dialogWindow.setAttributes(lp);
dialog.show();
}
});
}
- 运行结果