要在Android的DialogActivity中实现事件监听,可以通过以下步骤实现:
public class MyDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Dialog Title")
.setMessage("Dialog Message")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 处理点击OK按钮的事件
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 处理点击Cancel按钮的事件
}
});
return builder.create();
}
}
public class DialogActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "my_dialog");
}
}
通过以上步骤,就可以在DialogActivity中实现事件监听,当用户点击对话框中的按钮时,会触发相应的事件处理方法。