AlertDialog对话框的实现方法有以下几种:
1. 使用AlertDialog.Builder创建实例并设置属性,然后调用show方法显示对话框。示例代码如下:
```java
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("标题");
builder.setMessage("消息");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 点击确定按钮的逻辑处理
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 点击取消按钮的逻辑处理
}
});
AlertDialog dialog = builder.create();
dialog.show();
```
2. 使用AlertDialog的静态方法直接创建实例并设置属性,最后调用show方法显示对话框。示例代码如下:
```java
AlertDialog alertDialog = new AlertDialog.Builder(context)
.setTitle("标题")
.setMessage("消息")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 点击确定按钮的逻辑处理
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 点击取消按钮的逻辑处理
}
})
.create();
alertDialog.show();
```
. 创建一个继承自DialogFragment的子类,例如MyDialogFragment:
```java
public class MyDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 在这里创建AlertDialog实例,并设置标题、消息、按钮等属性
}
}
```
2. 在MyDialogFragment中重写onCreateDialog方法,创建AlertDialog实例,并设置标题、消息、按钮等属性:
```java
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("标题");
builder.setMessage("消息");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 点击确定按钮的逻辑处理
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 点击取消按钮的逻辑处理
}
});
Dialog dialog = builder.create();
return dialog;
}
```
3. 在Activity中使用MyDialogFragment:
```java
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.show(getSupportFragmentManager(), "my_dialog_fragment");
```
以下是代码重构后的内容:
```java
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MyDialogFragment extends DialogFragment {
private static final String TAG = "MyDialogFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mydialog, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.fragment_mydialog, null);
TextView titleView = (TextView) dialogView.findViewById(R.id.title);
TextView messageView = (TextView) dialogView.findViewById(R.id.message);
Button positiveButton = (Button) dialogView.findViewById(R.id.positive_button);
Button negativeButton = (Button) dialogView.findViewById(R.id.negative_button);
titleView.setText("标题");
messageView.setText("消息");
positiveButton.setText("确定");
negativeButton.setText("取消");
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 点击确定按钮的逻辑处理
Log.i(TAG, "点击了确定按钮");
}
});
negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 点击取消按钮的逻辑处理
Log.i(TAG, "点击了取消按钮");
}
});
builder.setView(dialogView);
setPositiveButton("确定", builder);
setNegativeButton("取消", builder);
show(getSupportFragmentManager(), "dialog");
}
}
```