将片段提交到DialogFragment

时间:2017-11-13 18:12:28

标签: java android android-fragments dialog android-dialogfragment

您好我将提交片段存入DialogFragment有问题 我有简单的dialogFragment ...

public class CustomCalendarDialog extends DialogFragment {

    public CustomCalendarDialog() {
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_Holo);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_custom_calendar, container);
        FrameLayout frame = view.findViewById(R.id.Dialogcontainer1);
        int id = frame.getId();

        FragmentManager fragmentManager = ((FragmentActivity) getActivity()).getSupportFragmentManager();
        Fragment customCalendarFragment = CustomCalendarFragment.newInstance();
        fragmentManager.beginTransaction().replace(id, customCalendarFragment );

        return view;
    }

}

并且让Fragment可以使用此行进行活动

Fragment customCalendarFragment = CustomCalendarFragment.newInstance();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(frameLayout.getId(), customCalendarFragment ).commit();

这条线在活动计划中运作良好。

现在我需要将此片段替换为Dialog / Dialog Fragment。

我有布局fragment_custom_calendar.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000ff">
    <FrameLayout
        android:id="@+id/Dialogcontainer1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</FrameLayout>

将布局膨胀到对话框

View view = inflater.inflate(R.layout.fragment_custom_calendar, container);

变量容器为空我不知道它是否正确。

查看确定,在屏幕上可以看到蓝色布局(#0000ff)但我想查看我的片段(自定义日历)

程序未在提交行崩溃

FrameLayout frame = view.findViewById(R.id.Dialogcontainer1);
    int id = frame.getId();
    FragmentManager fragmentManager = ((FragmentActivity) getActivity()).getSupportFragmentManager();
    Fragment customCalendarFragment  = CustomCalendarFragment.newInstance();
    fragmentManager.beginTransaction().replace(id, customCalendarFragment );

frame is not null id不为null customCalendarFragment不为null但我无法看到它。

我创建片段:

public static Fragment newInstance() {
    return (Fragment) new CustomCalendarFragment();
}

来自MainActivity的调用对话框

CustomCalendarDialog cdd = new CustomCalendarDialog();
    cdd.show(getFragmentManager(), "TAG");

我在对话框中需要这个片段:(我不知道提交片段是否在普通对话中是可行的,如果是,请链接我,我会看。

感谢任何想法

1 个答案:

答案 0 :(得分:0)

我用这里的活动解决了类似的问题。

https://stackoverflow.com/a/18234691/8250313

    /* Start Activity */ public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setClassName("com.thinoo.ActivityTest", "com.thinoo.ActivityTest.NewActivity");
    startActivityForResult(intent,90); } /* Called when the second activity's finished */ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
    case 90:
        if (resultCode == RESULT_OK) {
            Bundle res = data.getExtras();
            String result = res.getString("param_result");
            Log.d("FIRST", "result:"+result);
        }
        break;
    } }

private void finishWithResult() {
    Bundle conData = new Bundle();
    conData.putString("param_result", "Thanks Thanks");
    Intent intent = new Intent();
    intent.putExtras(conData);
    setResult(RESULT_OK, intent);
    finish(); }