简体   繁体   中英

AlertFragments with android support v4 library

I'm trying to create custom AlertDialog but I have one problem.

My alert dialog class below:

class AddNewDayAlert : DialogFragment
{
    public static AddNewDayAlert NewInstance(Bundle bundle)
    {
        AddNewDayAlert fragment = new AddNewDayAlert();
        fragment.Arguments = bundle;
        return fragment;
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        View view = inflater.Inflate(Resource.Layout.AddNewDayLayout, container, false);

        Button btn = view.FindViewById<Button>(Resource.Id.dialog_text);
        EditText txt = view.FindViewById<EditText>(Resource.Id.dialog_text);
        btn.Click += delegate
        {
            string day_name;
            if (txt.Text != "" && txt.Text != null)
            {
                day_name = txt.Text;
                Toast.MakeText(this.Activity, "Sėkmingai pridėta!!!", ToastLength.Long).Show();
                Dismiss();
            }

            else
                Toast.MakeText(this.Activity, "Pamiršote įvesti dienos pavadinimą!", ToastLength.Long).Show();
        };

        return view;
    }

}

and problem is here:

        Android.Support.V4.App.FragmentTransaction ft = FragmentManager.BeginTransaction();
        Fragment prev = FragmentManager.FindFragmentByTag("dialog");
        if (prev != null)
        {
            ft.Remove(prev);
        }
        ft.AddToBackStack(null);

        AddNewDayAlert fragmentas = AddNewDayAlert.NewInstance(null);
        fragmentas.Show(ft, "dialog");

In the last line I get an error because fragmentas.Show() is using Android.App.FragmentTransaction and I am using Android.Support.V4.App.FragmentTransaction .

If I try to use Android.App.FragmentTransaction I get error in first line: 在此处输入图片说明

Maybe there is way to convert? What should I do ?

You are conflicting two different FragmentTransaction classes. If you decide to use Support v4 Fragments , then you will want to use the Support v4 FragmentTransaction .

https://developer.android.com/reference/android/app/FragmentTransaction.html

https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html

You will also need to ensure that you are using a AppCompatActivity (Which inherits FragmentActivity and has a reference to Android.Support.V4.App.FragmentManager ) if you go down the v4 route. That way you will have access to SupportFragmentManager that is the same type.

EX:

[Activity(Label = "App7", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        // SetContentView (Resource.Layout.Main);

        Android.Support.V4.App.FragmentTransaction ft = SupportFragmentManager.BeginTransaction();

    }
}

您的AddNewDayAlert类也应继承自V4 DialogFragment

class AddNewDayAlert : Android.Support.V4.App.DialogFragment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM