从片段显示对话框?

我有一些片段需要显示一个常规的对话框。在这些对话框中,用户可以选择“是”或“否”,然后片段就应该有相应的行为。

现在,Fragment类没有要重写的 onCreateDialog()方法,所以我想我必须在包含 Activity的外部实现对话框。没关系,但是 Activity需要以某种方式向片段报告所选择的答案。当然,我可以在这里使用回调模式,这样片段就会在 Activity中用侦听器类注册自己,Activity 就会通过侦听器类或类似的东西来报告答案。

但是,对于在片段中显示“简单”的 yes-no 对话框这样的简单任务来说,这似乎是一个相当大的混乱。而且,这样我的 Fragment就不那么独立了。

有什么更干净的方法吗?

编辑:

这个问题的答案并没有详细解释应该如何使用 DialogFragments 来显示片段中的对话框。所以 AFAIK,方法是:

  1. 显示片段。
  2. When needed, instantiate a DialogFragment.
  3. 使用 .setTargetFragment()将原始片段设置为此 DialogFragment 的目标。
  4. 使用. Show ()从原始片段显示 DialogFragment。
  5. 当用户在这个 DialogFragment 上选择某个选项时,通知原始片段关于这个选项的信息(例如,用户单击“ yes”) ,您可以使用。GetTarget ().
  6. 解除对话框碎片。
141453 次浏览

您应该使用 对话片段代替。

我必须谨慎地怀疑以前接受的答案,即使用 DialogFragment 是最好的选择。DialogFragment 的预期(主要)用途似乎是显示 对话框本身的片段,而不是显示 对话框要显示的片段。

我相信使用片段的活动在对话框和片段之间进行调解是更好的选择。

下面是一个完整的对话框片段示例:

课程:

public class SomeDialog extends DialogFragment {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setMessage("Sure you wanna do this!")
.setNegativeButton(android.R.string.no, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do nothing (will close dialog)
}
})
.setPositiveButton(android.R.string.yes,  new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something
}
})
.create();
}
}

To start dialog:

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Create and show the dialog.
SomeDialog newFragment = new SomeDialog ();
newFragment.show(ft, "dialog");

您还可以让类实现 onClickListener,并使用它来代替嵌入式侦听器。

活动回拨

如果你想实现回调,这是如何做到这一点 在你的活动中:

YourActivity extends Activity implements OnFragmentClickListener

还有

@Override
public void onFragmentClick(int action, Object object) {
switch(action) {
case SOME_ACTION:
//Do your action here
break;
}
}

回调类:

public interface OnFragmentClickListener {
public void onFragmentClick(int action, Object object);
}

然后,要执行来自片段的回调,您需要确保监听器像下面这样附加:

@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement listeners!");
}
}

回调的执行方式如下:

mListener.onFragmentClick(SOME_ACTION, null); // null or some important object as second parameter.

对我来说,是这样的

MyFragment:

public class MyFragment extends Fragment implements MyDialog.Callback
{
ShowDialog activity_showDialog;


@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
activity_showDialog = (ShowDialog)activity;
}
catch(ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "ShowDialog interface needs to be     implemented by Activity.", e);
throw e;
}
}


@Override
public void onClick(View view)
{
...
MyDialog dialog = new MyDialog();
dialog.setTargetFragment(this, 1); //request code
activity_showDialog.showDialog(dialog);
...
}


@Override
public void accept()
{
//accept
}


@Override
public void decline()
{
//decline
}


@Override
public void cancel()
{
//cancel
}


}

我的对话:

public class MyDialog extends DialogFragment implements View.OnClickListener
{
private EditText mEditText;
private Button acceptButton;
private Button rejectButton;
private Button cancelButton;


public static interface Callback
{
public void accept();
public void decline();
public void cancel();
}


public MyDialog()
{
// Empty constructor required for DialogFragment
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.dialogfragment, container);
acceptButton = (Button) view.findViewById(R.id.dialogfragment_acceptbtn);
rejectButton = (Button) view.findViewById(R.id.dialogfragment_rejectbtn);
cancelButton = (Button) view.findViewById(R.id.dialogfragment_cancelbtn);
acceptButton.setOnClickListener(this);
rejectButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
getDialog().setTitle(R.string.dialog_title);
return view;
}


@Override
public void onClick(View v)
{
Callback callback = null;
try
{
callback = (Callback) getTargetFragment();
}
catch (ClassCastException e)
{
Log.e(this.getClass().getSimpleName(), "Callback of this class must be implemented by target fragment!", e);
throw e;
}


if (callback != null)
{
if (v == acceptButton)
{
callback.accept();
this.dismiss();
}
else if (v == rejectButton)
{
callback.decline();
this.dismiss();
}
else if (v == cancelButton)
{
callback.cancel();
this.dismiss();
}
}
}
}

活动:

public class MyActivity extends ActionBarActivity implements ShowDialog
{
..


@Override
public void showDialog(DialogFragment dialogFragment)
{
FragmentManager fragmentManager = getSupportFragmentManager();
dialogFragment.show(fragmentManager, "dialog");
}
}

对话框片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >


<TextView
android:id="@+id/dialogfragment_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:text="@string/example"/>


<Button
android:id="@+id/dialogfragment_acceptbtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/dialogfragment_textview"
android:text="@string/accept"
/>


<Button
android:id="@+id/dialogfragment_rejectbtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_alignLeft="@+id/dialogfragment_acceptbtn"
android:layout_below="@+id/dialogfragment_acceptbtn"
android:text="@string/decline" />


<Button
android:id="@+id/dialogfragment_cancelbtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_alignLeft="@+id/dialogfragment_rejectbtn"
android:layout_below="@+id/dialogfragment_rejectbtn"
android:text="@string/cancel" />


<Button
android:id="@+id/dialogfragment_heightfixhiddenbtn"
android:layout_width="200dp"
android:layout_height="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:layout_alignLeft="@+id/dialogfragment_cancelbtn"
android:layout_below="@+id/dialogfragment_cancelbtn"
android:background="@android:color/transparent"
android:enabled="false"
android:text=" " />
</RelativeLayout>

正如名称 dialogfragment_heightfixhiddenbtn显示,我只是不能找到一种方法来修复的底部按钮的高度被削减了一半,尽管说 wrap_content,所以我增加了一个隐藏按钮被“削减”一半而不是。抱歉我黑了你的电脑。

 public void showAlert(){




AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View alertDialogView = inflater.inflate(R.layout.test_dialog, null);
alertDialog.setView(alertDialogView);


TextView textDialog = (TextView) alertDialogView.findViewById(R.id.text_testDialogMsg);
textDialog.setText(questionMissing);


alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();


}

在哪里. test _ Dialogue 是 xml 自定义的

    public static void OpenDialog (Activity activity, DialogFragment fragment){


final FragmentManager fm = ((FragmentActivity)activity).getSupportFragmentManager();


fragment.show(fm, "tag");
}

我自己也是个初学者,老实说,我找不到一个我能理解或执行的令人满意的答案。

所以这里有一个外部链接,我真的帮助我实现了我想要的。它非常直接,也很容易理解。

http://www.helloandroid.com/tutorials/how-display-custom-dialog-your-android-application

这就是我试图通过代码实现的目标:

我有一个主活动(MainActivity) ,它托管一个片段。我想要一个对话框出现在布局的顶部,请求用户输入,然后相应地处理输入。 看截图

下面是我的片段的 onCreateView 的外观

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


View rootView = inflater.inflate(R.layout.fragment_home_activity, container, false);


Button addTransactionBtn = rootView.findViewById(R.id.addTransactionBtn);


addTransactionBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_trans);
dialog.setTitle("Add an Expense");
dialog.setCancelable(true);


dialog.show();


}
});

我希望这能帮到你

如果有任何疑问,请告诉我