Xamarin.Android 中是否存在 DisplayActionSheet?
Does DisplayActionSheet exist in Xamarin.Android?
我是移动开发新手。我正在尝试在 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>
您要查找的是 android 中的 AlertDialog Class :
A subclass of Dialog that can display one, two or three buttons.
以下是您可以用来实现它的详细指南:
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
祝你好运,编码愉快!
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");
我是移动开发新手。我正在尝试在 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>
您要查找的是 android 中的 AlertDialog Class :
A subclass of Dialog that can display one, two or three buttons.
以下是您可以用来实现它的详细指南:
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
祝你好运,编码愉快!
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");