问题
I am new to mobile development. I'm trying to write an app in both Xamarin iOS and Xamarin Android. After searching for a couple of days, I keep finding that DisplayActionSheet is part of Xamarin Forms, but I can't find the equivalent in Xamarin Android. Does DisplayActionSheet not exist? Is there anything else I can use to get the same look and effect?
Thank you!
Update: Here's my progress... I'm trying to make a dialog that looks like this the image here: 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: So far, I can get the image to slide up and the buttons to respond correctly, but the one thing I'm missing is the behavior when the user clicks outside of the dialog. I want it to be dismissed, but nothing is happening. Thank you for your help!
Here is my code:
//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:
What you are looking for is AlertDialog Class in android :
A subclass of Dialog that can display one, two or three buttons.
Following are the detailed guide you can work with to achieve it :
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
For Customization of this you can use the following :
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;
}
}
You are not calling the constructor which is causing the issue initializing it like how I have shown you will do it.
And this is the code for calling the actual dialog:
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");
来源:https://stackoverflow.com/questions/49492018/does-displayactionsheet-exist-in-xamarin-android