一、对话框Dialog
1、简介
对话框Dialog是人与应用交互的窗口,只要涉及交互的地方都可以使用对话框。
一个对话框一般是漂浮于当前Activity之上的小窗口,当前Activity失去焦点
一、AlertDiaLog 警报对话框
1、AlertDialog一共有0-3个按钮,一个单选框或者复选框列表的对话框。警告对话框可以适应大多数界面交互的需求。
2、AlertDialog的实现
要实例化AlertDialog,不能直接通过AlertDialog的构造方法实例化,需要通过AlertDilog.Builder的create()方法来实例化
public class MainActivity extends AppCompatActivity {
Button mButton;
AlertDialog mAlertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.start_dialog_btn);
//我们通过点击一个按钮弹出对话框
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAlertDialog.show();
}
});
//Builder是AlertDialog的一个内部类,在用法上个人感觉有点类似于适配器,都是使控件关联数据
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置对话框标题显示的文本信息
builder.setTitle("AlertDialog对话框");
//设置对话框中间的提示信息
builder.setMessage("是否退出?");
//NegativeButton一般是在最左边
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "你点击了\"取消\"", Toast.LENGTH_LONG).show();
}
});
//NegativeButton一般是在中间位置
builder.setNeutralButton("none", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "你点击了\"none\"", Toast.LENGTH_LONG).show();
}
});
//NegativeButton一般是在最右边的位置
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "你点击了\"确定\"", Toast.LENGTH_LONG).show();
}
});
mAlertDialog = builder.create();
}
}
3、自定义AlertDialog
系统自带的对话框满足不了交互的需求,此时我们可以自定义对话框,使其中的内容不再只是仅仅只有纯文本、单选框或复选框。
新建一个类继承AlertDialog类,重写里面的onCreate()方法
public class MyAlertDialog extends AlertDialog {
protected MyAlertDialog(Context context) {
super(context);
}
protected MyAlertDialog(Context context, int theme) {
super(context, theme);
}
protected MyAlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
//上面的构造方法不用管,我们只需要重写onCreate()方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载对话框的布局文件,布局文件由我们自定义,需要什么就怎么设置
setContentView(R.layout.my_alert_dialog_layout);
}
}
再到Activity中实例化,并调用.show()方法显示
public class MainActivity extends AppCompatActivity {
Button mButton;
AlertDialog mAlertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.start_dialog_btn);
final MyAlertDialog myAlertDialog = new MyAlertDialog(this);
//我们通过点击一个按钮弹出对话框
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myAlertDialog.show();
}
});
}
}
我的布局文件是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<TextView
android:id="@+id/user_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="24sp" />
<EditText
android:id="@+id/user_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/user_txt" />
<TextView
android:id="@+id/password_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user_txt"
android:layout_marginTop="20dp"
android:text="密码:"
android:textSize="24sp" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/user_edittext"
android:layout_below="@id/user_edittext" />
</RelativeLayout>