下面列出了怎么用android.app.DialogFragment的API类实例代码及写法,或者点击链接到github查看源代码。
@OptionsItem(R.id.menu_connection)
public void onConnectionInfo() {
AllAccounts allAccounts = rxStore.getAllAccountsWrapped();
ArrayList<String> names = new ArrayList<>();
ArrayList<String> errors = new ArrayList<>();
for (ConnectionInfo info : failedInfos) {
final MovirtAccount account = allAccounts.getAccountById(info.getAccountId());
if (account == null) {
continue;
}
names.add(account.getName());
errors.add(info.getMessage(this));
}
DialogFragment dialogFragment = ConnInfoDialogFragment.newInstance(names, errors);
dialogFragment.show(getFragmentManager(), "connection_info");
}
@Override
public void onStart() {
super.onStart();
// Change window title
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
getActivity().setTitle(getResources().getString(R.string.app_name));
else
getActivity().setTitle(" " + getResources().getString(R.string.app_name));
// Don't show the Up button in the action bar, and disable the button
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(false);
// Floating action button
FloatingActionButton floatingActionButton = getActivity().findViewById(R.id.button_floating_action_welcome);
floatingActionButton.setImageResource(R.drawable.ic_action_new);
floatingActionButton.setOnClickListener(v -> {
DialogFragment newProfileFragment = new NewProfileDialogFragment();
newProfileFragment.show(getFragmentManager(), "new");
});
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Capture UI elements
mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
mPickDate = (Button) findViewById(R.id.pickDate);
// Set an OnClickListener for the Change the Date Button
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Create a new DatePickerFragment
DialogFragment newFragment = new DatePickerFragment();
// Display DatePickerFragment
newFragment.show(getFragmentManager(), "DatePicker");
}
});
}
/**
* The DashboardFragment wants the MainActivity to know that the user has
* interacted with it. It's up to the MainActivity to take action, leaving
* the Fragment uncluttered.
*
* @param item The item that was touched by the user and that this method needs
* to do something with.
*/
@Override
public void onDashboardFragmentInteraction(IotDeviceContent.IotDeviceItem item) {
Toast.makeText(getApplicationContext(), "Howdy from IotDeviceItem: " + item.toString(), Toast.LENGTH_LONG).show();
// Display the SelectAction DialogFragment to let the user pick the action they
/// want to invoke. For now, all actions must be no-arg.
List<Pair<String, String>> actions = item.actions;
String[] actionsArray = new String[actions.size()];
int aa = 0; // It's a throw-away variable. Get over it.
for (Pair<String, String> action : actions) {
actionsArray[aa++] = action.getLeft() + ":=> " + action.getRight();
}
DialogFragment dialogFragment = ActionDialogFragment.newInstance(item.deviceId, actionsArray);
dialogFragment.show(getFragmentManager(), ActionDialogFragment.FRAGMENT_TAG);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
String policy = sharedPref.getString(
SettingsActivity.KEY_IMPORT_MERGE_POLICY,
getResources().getString(R.string.setting_import_default_value));
setRetainInstance(true);
ImportTask task = new ImportTask(getArguments().getString(ARG_DATA), policy);
_taskRef = new WeakReference<ImportTask>(task);
task.execute();
setCancelable(false);
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
}
void showDialog() {
mStackLevel++;
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");
}
@Override
public void onSignOutClicked() {
// In case the user reached this fragment without being signed in, we guard the sign out so
// we do not hit a native crash.
if (!ChromeSigninController.get(getActivity()).isSignedIn()) return;
final Activity activity = getActivity();
final DialogFragment clearDataProgressDialog = new ClearDataProgressDialog();
SigninManager.get(activity).signOut(null, new SigninManager.WipeDataHooks() {
@Override
public void preWipeData() {
clearDataProgressDialog.show(
activity.getFragmentManager(), CLEAR_DATA_PROGRESS_DIALOG_TAG);
}
@Override
public void postWipeData() {
if (clearDataProgressDialog.isAdded()) {
clearDataProgressDialog.dismissAllowingStateLoss();
}
}
});
AccountManagementScreenHelper.logEvent(
ProfileAccountManagementMetrics.SIGNOUT_SIGNOUT,
mGaiaServiceType);
}
@Override
public void onSignOutClicked() {
// In case the user reached this fragment without being signed in, we guard the sign out so
// we do not hit a native crash.
if (!ChromeSigninController.get().isSignedIn()) return;
final Activity activity = getActivity();
final DialogFragment clearDataProgressDialog = new ClearDataProgressDialog();
SigninManager.get(activity).signOut(null, new SigninManager.WipeDataHooks() {
@Override
public void preWipeData() {
clearDataProgressDialog.show(
activity.getFragmentManager(), CLEAR_DATA_PROGRESS_DIALOG_TAG);
}
@Override
public void postWipeData() {
if (clearDataProgressDialog.isAdded()) {
clearDataProgressDialog.dismissAllowingStateLoss();
}
}
});
AccountManagementScreenHelper.logEvent(
ProfileAccountManagementMetrics.SIGNOUT_SIGNOUT,
mGaiaServiceType);
}
@Override
public void onSignOutClicked() {
// In case the user reached this fragment without being signed in, we guard the sign out so
// we do not hit a native crash.
if (!ChromeSigninController.get(getActivity()).isSignedIn()) return;
final Activity activity = getActivity();
final DialogFragment clearDataProgressDialog = new ClearDataProgressDialog();
SigninManager.get(activity).signOut(null, new SigninManager.WipeDataHooks() {
@Override
public void preWipeData() {
clearDataProgressDialog.show(
activity.getFragmentManager(), CLEAR_DATA_PROGRESS_DIALOG_TAG);
}
@Override
public void postWipeData() {
if (clearDataProgressDialog.isAdded()) {
clearDataProgressDialog.dismissAllowingStateLoss();
}
}
});
AccountManagementScreenHelper.logEvent(
ProfileAccountManagementMetrics.SIGNOUT_SIGNOUT,
mGaiaServiceType);
}
/**
* Display an error and either go back to the current activity or finish the current activity.
*
* @param activity the current activity
* @param resource the error message
* @param finishActivity a flag indicating if the activity should be finished.
* @param args arguments for the error message
*/
public static void displayError(@NonNull final Activity activity, final int resource, final boolean finishActivity,
final Object... args) {
MessageDialogListener listener = null;
if (finishActivity) {
listener = new MessageDialogListener() {
/**
* The serial version id.
*/
private static final long serialVersionUID = 1L;
@Override
public void onDialogClick(final DialogFragment dialog) {
activity.finish();
}
@Override
public void onDialogCancel(final DialogFragment dialog) {
activity.finish();
}
};
}
displayError(activity, resource, listener, args);
}
@OnClick({R.id.fab_scoll_top, R.id.fab_create_file, R.id.fab_create_floder})
public void onClick(View view) {
switch (view.getId()) {
case R.id.fab_scoll_top:
recyclerView.smoothScrollToPosition(0);
floatingMenu.close(true);
break;
case R.id.fab_create_file:
floatingMenu.close(true);
DialogFragment fileDialog = CreateFileDialog.create(path);
fileDialog.show(getActivity().getFragmentManager(), DIALOGTAG);
break;
case R.id.fab_create_floder:
floatingMenu.close(true);
DialogFragment folderDialog = CreateFolderDialog.create(path);
folderDialog.show(getActivity().getFragmentManager(), DIALOGTAG);
break;
}
}
/**
* Display the info of this photo.
*
* @param activity the triggering activity
* @param eyePhoto the photo for which the image should be displayed.
*/
public static void displayImageInfo(@NonNull final Activity activity, @NonNull final EyePhoto eyePhoto) {
StringBuilder message = new StringBuilder();
message.append(formatImageInfoLine(activity, R.string.imageinfo_line_filename, eyePhoto.getFilename()));
message.append(formatImageInfoLine(activity, R.string.imageinfo_line_filedate, eyePhoto.getDateString(activity)));
try {
JpegMetadata metadata = JpegSynchronizationUtil.getJpegMetadata(eyePhoto.getAbsolutePath());
if (metadata.getPerson() != null && metadata.getPerson().length() > 0) {
message.append(formatImageInfoLine(activity, R.string.imageinfo_line_name, metadata.getPerson()));
}
if (metadata.getComment() != null && metadata.getComment().length() > 0) {
message.append(formatImageInfoLine(activity, R.string.imageinfo_line_comment, metadata.getComment()));
}
}
catch (Exception e) {
// cannot append metadata.
}
Bundle bundle = new Bundle();
bundle.putCharSequence(PARAM_MESSAGE, fromHtml(message.toString()));
bundle.putString(PARAM_TITLE, activity.getString(R.string.title_dialog_image_info));
bundle.putInt(PARAM_ICON, R.drawable.ic_title_info);
DialogFragment fragment = new DisplayMessageDialogFragment();
fragment.setArguments(bundle);
fragment.show(activity.getFragmentManager(), fragment.getClass().toString());
}
private void showDialog() {
final String[] suggestions = (getIntent() != null && getIntent().hasExtra(EXTRA_QUERY_SUGGESTIONS))
? getIntent().getStringArrayExtra(EXTRA_QUERY_SUGGESTIONS) : new String[]{};
DialogFragment fragment = ShowRecordLogDialog.newInstance(suggestions);
fragment.show(getFragmentManager(), "showRecordLogDialog");
}
@Override
public void onDialogNegativeClick(DialogFragment dialog, int index) {
if(index > 0) {
adapter.remove(index);
}
dialog.dismiss();
}
public static void showDialog(FragmentManager fm, String currentPath, String fileName)
{
DialogFragment newFragment = new RenameFileDialog();
Bundle b = new Bundle();
b.putString(ARG_CURRENT_PATH, currentPath);
b.putString(ARG_FILENAME, fileName);
newFragment.setArguments(b);
newFragment.show(fm, TAG);
}
public static void showDialog(FragmentManager fm, int type, String receiverTag)
{
DialogFragment newFragment = new NewFileDialog();
Bundle b = new Bundle();
b.putInt(ARG_TYPE, type);
b.putString(ARG_RECEIVER_TAG, receiverTag);
newFragment.setArguments(b);
newFragment.show(fm, "NewFileDialog");
}
void showDialog(String text) {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = MyDialogFragment.newInstance(text);
// Show the dialog.
newFragment.show(ft, "dialog");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, 0);
if (getArguments().containsKey(ARG_END_POINT_DEFAULT)) {
mEndPointDefault = getArguments().getString(ARG_END_POINT_DEFAULT);
} else {
mEndPointDefault = null;
}
}
private static void showDialog(Activity activity, Class clazz, String tag) {
FragmentManager fm = activity.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment prev = fm.findFragmentByTag(tag);
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
try {
((DialogFragment) clazz.newInstance()).show(ft, tag);
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
}
private void showAddDialog(int index, String word) {
DialogFragment newFragment = new AddWordDialogFragment();
Bundle args = new Bundle();
args.putInt("index", index);
args.putString("word", word);
newFragment.setArguments(args);
newFragment.show(getFragmentManager(), "new_word");
}
@ReactMethod
public void showDatepickerWithInitialDateInMilliseconds(String initialDateString, Callback errorCallback,
Callback successCallback) {
DialogFragment dateDialog = new DatePicker(DateFormatHelper.parseDateInMilliseconds(Long.parseLong(initialDateString)),
errorCallback, successCallback);
Activity activity = getCurrentActivity();
if (activity != null) {
dateDialog.show(activity.getFragmentManager(), "datePicker");
}
}
public static void showDialog(FragmentManager fm, int propertyId, String title, List<String> variants, String hostFragmentTag)
{
Bundle args = new Bundle();
args.putStringArrayList(ARG_VARIANTS, new ArrayList<>(variants));
args.putString(ARG_TITLE, title);
args.putInt(PropertyEditor.ARG_PROPERTY_ID, propertyId);
if(hostFragmentTag!=null)
args.putString(PropertyEditor.ARG_HOST_FRAGMENT_TAG, hostFragmentTag);
DialogFragment newFragment = new ChoiceDialog();
newFragment.setArguments(args);
newFragment.show(fm, TAG);
}
@ReactMethod
public void showTimepickerWithInitialTime(String initialDateString, Callback errorCallback,
Callback successCallback) {
DialogFragment dateDialog = new TimePicker(DateFormatHelper.parseDate(initialDateString),
errorCallback, successCallback);
Activity activity = getCurrentActivity();
if (activity != null) {
dateDialog.show(activity.getFragmentManager(), "timePicker");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Do not create a new Fragment when the Activity is re-created such as orientation changes.
setRetainInstance(true);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Material_Light_Dialog);
mKeyguardManager = (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE);
mFingerprintUiHelperBuilder = new FingerprintUiHelper.FingerprintUiHelperBuilder(
getContext(), getContext().getSystemService(FingerprintManager.class));
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Do not create a new Fragment when the Activity is re-created such as orientation changes.
setRetainInstance(true);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Material_Light_Dialog);
mKeyguardManager = (KeyguardManager) getContext().getSystemService(Context.KEYGUARD_SERVICE);
mFingerprintUiHelperBuilder = new FingerprintUiHelper.FingerprintUiHelperBuilder(
getContext(), getContext().getSystemService(FingerprintManager.class));
}
/**
* Factory for an alert dialog
* @param s the title and message of the new dialog
* @param manager the fragment manager used to show the dialog
*/
public static void newInstance(String s, FragmentManager manager) {
DialogFragment f = new GenericAlertDialogFragment();
Bundle args = new Bundle();
args.putString("text", s);
f.setArguments(args);
f.show(manager, "dialog");
}
public static void showAboutDialog(Activity activity) {
FragmentManager fm = activity.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
((DialogFragment) Fragment
.instantiate(activity, AboutDialogFragment.class.getName()))
.show(ft, ABOUT_DIALOG_TAG);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Do not create a new Fragment when the Activity is re-created such as orientation changes.
setRetainInstance(true);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog);
}
public static void showHelpDialog(Activity activity) {
FragmentManager fm = activity.getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
((DialogFragment) Fragment
.instantiate(activity, HelpDialogFragment.class.getName()))
.show(ft, ABOUT_DIALOG_TAG);
}
private OAuthDialogFragment(android.app.DialogFragment fragment, boolean fullScreen,
boolean horizontalProgress, boolean hideFullScreenTitle) {
super(fragment);
this.mFullScreen = fullScreen;
this.mHorizontalProgress = horizontalProgress;
this.mHideFullScreenTitle = hideFullScreenTitle;
}