gpt4 book ai didi

android - 全屏 DialogFragment 关闭 Activity

转载 作者:行者123 更新时间:2023-11-29 01:16:02 24 4
gpt4 key购买 nike

我正在为表单输入的 Activity 开发全屏 DialogFragment,使用这两个其他问题作为主要来源: How to achieve a full-screen dialog as described in material guidelines?Android full screen dialog appears transparent and in the wrong position .

每当我按下 DialogFragment 中的关闭按钮(向上按钮)时,它后面的 Activity 也被关闭。第二个问题的答案解决了布局的其余部分让压力机通过的问题,但不是工具栏。

fragment_dialog_add_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:background="@color/md_white">

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<android.support.constraint.ConstraintLayout
android:id="@+id/dialogLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toTopOf="parent"
android:hint="@string/hint_name">

<android.support.design.widget.TextInputEditText
android:id="@+id/etxt_restaurant_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1"
android:inputType="text" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/textInputLayout"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/textInputLayout2"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:hint="@string/hint_location">

<android.support.design.widget.TextInputEditText
android:id="@+id/etxt_restaurant_location"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

AddEditDialogFragment.java

package nl.verhoogenvansetten.restaurantrio;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

public class AddEditDialogFragment extends DialogFragment {
private static final String TAG = "AddEditDialogFragment";

public AddEditDialogFragment() {}

public static AddEditDialogFragment newInstance(String title) {
AddEditDialogFragment frag = new AddEditDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
frag.setArguments(args);
return frag;
}

public static AddEditDialogFragment newInstance(String title, String name, String location, byte[] image) {
AddEditDialogFragment frag = new AddEditDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("name", name);
args.putString("location", location);
args.putByteArray("image", image);
frag.setArguments(args);
return frag;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dialog_add_edit, container, false);

Bundle args = getArguments();

Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
String mTitle = getArguments().getString("title");
toolbar.setTitle(mTitle);

String mName;
TextInputEditText mETxtName = (TextInputEditText) rootView.findViewById(R.id.etxt_restaurant_name);
if ((mName = args.getString("name")) != null)
mETxtName.setText(mName);

String mLocation;
TextInputEditText mETxtLocation = (TextInputEditText) rootView.findViewById(R.id.etxt_restaurant_location);
if ((mLocation = args.getString("location")) != null)
mETxtLocation.setText(mLocation);

AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);

ActionBar actionBar = activity.getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_close_white_24dp);
}

setHasOptionsMenu(true);
return rootView;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
getActivity().getMenuInflater().inflate(R.menu.menu_add_edit_dialog, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_save) {
// Save data to DB
return true;
} else if (id == android.R.id.home) {
getActivity().onBackPressed();
return true;
}

return super.onOptionsItemSelected(item);
}
}

为了打开对话框,创建了一个实用程序类:

DialogUtil.java

package nl.verhoogenvansetten.restaurantrio.util;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import nl.verhoogenvansetten.restaurantrio.AddEditDialogFragment;

/**
* Created by Bas on 21-9-2016.
*/

public class DialogUtil {
public static void openAddDialog(FragmentManager fragmentManager, String title) {
AddEditDialogFragment addFragment = AddEditDialogFragment.newInstance(title);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, addFragment, "add_edit_fragment").addToBackStack(null).commit();
}

public static void openEditDialog(FragmentManager fragmentManager, String title, String name, String location, byte[] image) {
AddEditDialogFragment addFragment = AddEditDialogFragment.newInstance(title, name, location, image);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.add(android.R.id.content, addFragment, "add_edit_fragment").addToBackStack(null).commit();
}
}

最佳答案

在 fragment 的 onOptionsItemSelected 中弹出 backstack

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_save) {
// Save data to DB
return true;
} else if (id == android.R.id.home) {
// pop the back stack to remove this dialog
FragmentManager manager = getFragmentManager();
manager.popBackStack();
manager.executePendingTransactions()
return true;
}

return super.onOptionsItemSelected(item);
}

关于android - 全屏 DialogFragment 关闭 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39620410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com