要在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中實現事件監聽,當用戶點擊對話框中的按鈕時,會觸發相應的事件處理方法。