DisplayActionSheet是否存在于Xamarin.Android中?

时间:2018-03-26 12:56:00

标签: xamarin.android dialogfragment

我是移动开发的新手。我正在尝试在Xamarin iOS和Xamarin Android中编写应用程序。搜索了几天之后,我一直发现DisplayActionSheet是Xamarin Forms的一部分,但我找不到Xamarin Android中的等价物。 DisplayActionSheet不存在吗?还有什么我可以用来获得相同的外观和效果吗?

谢谢!

更新:这是我的进步...... 我正在尝试在此处创建一个如下图所示的对话框:https://www.google.com/search?q=displayactionsheet+image&rlz=1C1GGRV_enUS752US754&tbm=isch&source=iu&ictx=1&fir=EWVc1vOm1JJrhM%253A%252CYaPWhxv5zFphhM%252C_&usg=__v_-JK4lHAj8k50h5iknuPLPc8PM%3D&sa=X&ved=0ahUKEwjbz66zzZHaAhUKY6wKHXyxB64Q9QEIKTAA#imgrc=EWVc1vOm1JJrhM: 到目前为止,我可以让图像向上滑动,按钮可以正确响应,但我缺少的一件事就是用户在对话框外点击时的行为。我希望它被解雇,但一切都没有发生。谢谢你的帮助!

这是我的代码:

//MyActivity.cs

public class MyActivity : Activity
{
    FrameLayout _fragmentContainer;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.MyView);
        var listView = FindViewById<ListView>(Resource.Id.myView);
        listView.Adapter = new EmailSettingsAdapter(this);
        TextView action = FindViewById<TextView>(Resource.Id.actionText);

        _fragmentContainer = FindViewById<FrameLayout>(Resource.Id.frameContainer);

        action.Click += click;
    }

    private void click(object sender, EventArgs e)
    {
        FragmentTransaction transaction =     FragmentManager.BeginTransaction();
        MonthlyStmtFragment fragment = new MonthlyStmtFragment();
        transaction.Add(_fragmentContainer.Id, fragment, "Fragment");
        transaction.Commit();
        _fragmentContainer.TranslationY = 1800;
        _fragmentContainer.Animate().TranslationYBy(-600).SetDuration(500);
    }
}

//MyFragment.cs
public class MyFragment : DialogFragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        View view = inflater.Inflate(Resource.Layout.FragmentLayout, container, false);

        TextView text1 = view.FindViewById<TextView>(Resource.Id.text1);
        text1.Click += delegate {
            Dismiss();
            Toast.MakeText(Activity, "selected", ToastLength.Short).Show();
        };

        TextView cancel = view.FindViewById<TextView>(Resource.Id.cancelText);
        cancel.Click += delegate {
            Dismiss();
            Toast.MakeText(Activity, "cancel", ToastLength.Short).Show();
        };

        return view;
    }

//FragmentLayout.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<TextView
    android:text="Title Here"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/titleView1"
    android:gravity="center_horizontal"
    android:background="#fff5f5f5"
    android:textSize="8dp"
    android:textColor="#ff9e9e9e"
    android:padding="10dp" />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:id="@+id/view1"
    android:background="#ffbdbdbd" />
<TextView
    android:text="Text 1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:background="#fff5f5f5"
    android:gravity="center_horizontal"
    android:textColor="#ff1e88e5"
    android:padding="10dp"
    android:textSize="10dp" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/cancelText"
    android:background="#fff5f5f5"
    android:textSize="10dp"
    android:text="Cancel"
    android:textColor="#ff1e88e5"
    android:padding="10dp"
    android:gravity="center_horizontal"
    android:layout_marginTop="10dp" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

你要找的是android中的AlertDialog类:

  

Dialog的子类,可以显示一个,两个或三个按钮。

以下是您可以使用的详细指南:

https://www.c-sharpcorner.com/blogs/how-to-show-alert-dialog-in-android-using-xamarin

http://stacktips.com/tutorials/xamarin/alertdialog-and-dialogfragment-example-in-xamarin-android

对于自定义,您可以使用以下内容:

http://www.appliedcodelog.com/2015/11/custom-alertdialog-example-in.html

Goodluck,Happy Coding!

 class DialogFragmentExitPopUp : DialogFragment
{
    public static Activity mActivity;
    public DialogFragmentExitPopUp NewInstance(Activity activity,Bundle bundle)
    {
        var fragment = new DialogFragmentExitPopUp();
        fragment.Arguments = bundle;
        DialogFragmentExitPopUp.mActivity = activity;
        return fragment;
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState)
    {
        View view = inflater.Inflate(Resource.Layout.ExitPopUp, container, false);
        Button ButtonYes = view.FindViewById<Button>(Resource.Id.ExitYes);
        Button ButtonNo = view.FindViewById<Button>(Resource.Id.ExitNo);
        ButtonYes.SetTextColor(Color.ParseColor("#2466A3"));
        ButtonNo.SetTextColor(Color.ParseColor("#2466A3"));
        ButtonYes.SetBackgroundColor(Color.White);
        ButtonNo.SetBackgroundColor(Color.White);
        Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
        Dialog.SetCanceledOnTouchOutside(false);

        return view;
    }

}

你没有调用导致问题初始化的构造函数,就像我表明你会这样做。

这是调用实际对话框的代码:

 FragmentTransaction fragmentTransaction = FragmentManager.BeginTransaction();
        Fragment fragmentPrev = FragmentManager.FindFragmentByTag("dialog");
        if (fragmentPrev != null)
            fragmentTransaction.Remove(fragmentPrev);

        fragmentTransaction.AddToBackStack(null);
        //create and show the dialog
        DialogFragmentExitPopUp dialogFragment = new DialogFragmentExitPopUp().NewInstance(this, null);
        dialogFragment.Show(fragmentTransaction, "dialog");