我在Xamrin Android应用程序中创建了对话框片段。 在按钮上点击主布局,我打开了拨号片段。在拨号片段活动创建时,我隐藏(隐藏)主页上的所有按钮(在我的应用程序中完美运行)。 片段解散事件我需要使主布局上的所有项目都可见。我在使用OnDismiss()事件时遇到错误。
Masinactivity.cs
using Android.App;
using Android.Widget;
using Android.OS;
using System;
using Android.Content;
using Android.Media;
using Android.Views;
namespace MyHome
{
[Activity(Label = "MyHome", MainLauncher = true,Icon ="@drawable/Icon")]
public class MainActivity : Activity
{
private Button mbtnGoToRoom;
private Button mbtnGoToSettings;
private Button mBtnOverview;
private ToggleButton mBtnStatus;
private ProgressBar mprogressBar;
protected override void OnRestart()
{
base.OnRestart();
mbtnGoToRoom.Visibility = ViewStates.Visible;
mBtnOverview.Visibility = ViewStates.Visible;
mbtnGoToSettings.Visibility = ViewStates.Visible;
mBtnStatus.Visibility = ViewStates.Visible;
mprogressBar.Visibility = ViewStates.Visible;
var mClock = FindViewById<DigitalClock>(Resource.Id.digitalClock1);
mClock.Visibility = ViewStates.Visible;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mbtnGoToRoom = FindViewById<Button>(Resource.Id.BtnGoToRoom);
mBtnOverview = FindViewById<Button>(Resource.Id.BtnOverview);
mbtnGoToSettings = FindViewById<Button>(Resource.Id.BtnSetting);
mBtnStatus = FindViewById<ToggleButton>(Resource.Id.BtnStatus);
mprogressBar = FindViewById<ProgressBar>(Resource.Id.progressBar1);
var mClock = FindViewById<DigitalClock>(Resource.Id.digitalClock1);
mbtnGoToRoom.Click += (object sender, EventArgs args) =>
{
mbtnGoToRoom.Visibility = ViewStates.Invisible;
mBtnOverview.Visibility = ViewStates.Invisible;
mbtnGoToSettings.Visibility = ViewStates.Invisible;
mBtnStatus.Visibility = ViewStates.Invisible;
mprogressBar.Visibility = ViewStates.Invisible;
mClock.Visibility = ViewStates.Invisible;
//Pull up Room menu
FragmentTransaction transaction = FragmentManager.BeginTransaction();
Rooms room = new Rooms();
room.Show(transaction, "dialog fragment");
};
mbtnGoToSettings = FindViewById<Button>(Resource.Id.BtnSetting);
mbtnGoToSettings.Click += (object sender, EventArgs args) =>
{
//Pull up Room menu
FragmentTransaction transaction = FragmentManager.BeginTransaction();
Settings settings = new Settings();
settings.Show(transaction, "dialog fragment");
};
}
}
}
Rooms.cs(对话片段活动)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using static Android.Provider.CalendarContract;
namespace MyHome
{
class Rooms : DialogFragment
{
Android.Media.MediaPlayer player;
private Button mBtnGoToHall;
private Button mBtnGoToBroom1;
private Button mBtnGoToBroom2;
private Button mBtnGoToKitchen;
private Button mBtnGoToBalcony;
private Button mBtnGoToGarden;
private Button mbtnGoToRoom;
private Button mbtnGoToSettings;
private Button mBtnOverview;
private ToggleButton mBtnStatus;
private ProgressBar mprogressBar;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.Room_Menu, container, false);
mBtnGoToHall = view.FindViewById<Button>(Resource.Id.btnHall);
mBtnGoToHall.Click += mBtnGoToHall_Click;
mBtnGoToBroom1 = view.FindViewById<Button>(Resource.Id.btnBedroom1);
mBtnGoToBroom1.Click += mBtnGoToBroom1_Click;
mBtnGoToBroom2 = view.FindViewById<Button>(Resource.Id.btnBedroom2);
mBtnGoToBroom2.Click += mBtnGoToBroom2_Click;
mBtnGoToKitchen = view.FindViewById<Button>(Resource.Id.btnKitchen);
mBtnGoToKitchen.Click += mBtnGoToKitchen_Click;
mBtnGoToBalcony = view.FindViewById<Button>(Resource.Id.btnBalcony);
mBtnGoToBalcony.Click += mBtnGoToBalcony_Click;
mBtnGoToGarden = view.FindViewById<Button>(Resource.Id.btnGarden);
mBtnGoToGarden.Click += mBtnGoToGarden_Click;
return view;
}
private void mBtnGoToHall_Click(object sender, EventArgs e)
{
player.Start();
var intent = new Intent(Activity, typeof(ActvtHall));
StartActivity(intent);
this.Dismiss();
}
private void mBtnGoToBroom1_Click(object sender, EventArgs e)
{
player.Start();
var intent = new Intent(Activity, typeof(ActvtBedroom1));
StartActivity(intent);
this.Dismiss();
}
private void mBtnGoToBroom2_Click(object sender, EventArgs e)
{
player.Start();
var intent = new Intent(Activity, typeof(ActvtBedroom2));
StartActivity(intent);
this.Dismiss();
}
private void mBtnGoToKitchen_Click(object sender, EventArgs e)
{
player.Start();
var intent = new Intent(Activity, typeof(ActvtKitchen));
StartActivity(intent);
this.Dismiss();
}
private void mBtnGoToBalcony_Click(object sender, EventArgs e)
{
player.Start();
var intent = new Intent(Activity, typeof(ActvtBalcony));
StartActivity(intent);
this.Dismiss();
}
private void mBtnGoToGarden_Click(object sender, EventArgs e)
{
player.Start();
var intent = new Intent(Activity, typeof(ActvtGarden));
StartActivity(intent);
this.Dismiss();
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
player = Android.Media.MediaPlayer.Create(Context, Resource.Raw.Door);
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
base.OnActivityCreated(savedInstanceState);
Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation; //set the animation
Dialog.Window.SetBackgroundDrawable(new ColorDrawable(Color.Transparent));
}
public override void OnDismiss(IDialogInterface dialog)
{
base.OnDismiss(dialog);
mbtnGoToRoom = View.FindViewById<Button>(Resource.Id.BtnGoToRoom);
mBtnOverview = View.FindViewById<Button>(Resource.Id.BtnOverview);
mbtnGoToSettings = View.FindViewById<Button>(Resource.Id.BtnSetting);
mBtnStatus = View.FindViewById<ToggleButton>(Resource.Id.BtnStatus);
mprogressBar = View.FindViewById<ProgressBar>(Resource.Id.progressBar1);
var mClock = View.FindViewById<DigitalClock>(Resource.Id.digitalClock1);
mbtnGoToRoom.Visibility = ViewStates.Visible;
mBtnOverview.Visibility = ViewStates.Visible;
mbtnGoToSettings.Visibility = ViewStates.Visible;
mBtnStatus.Visibility = ViewStates.Visible;
mprogressBar.Visibility = ViewStates.Visible;
mClock.Visibility = ViewStates.Visible;
}
}
}
答案 0 :(得分:0)
原因是您无法在OnDismiss()
中获取按钮。我认为你应该在创建它时将它们传递到房间类中
例如,在MainActivity中:
FragmentTransaction transaction = FragmentManager.BeginTransaction();
Rooms room = new Rooms(mbtnGoToRoom, mbtnGoToSettings, mBtnOverview, mBtnStatus, mprogressBar, mClock);
room.Show(transaction, "dialog fragment");
在Rooms.cs中:
private Button mbtnGoToRoom;
private Button mbtnGoToSettings;
private Button mBtnOverview;
private ToggleButton mBtnStatus;
private ProgressBar mprogressBar;
private DigitalClock mClock;
public Rooms(Button mbtnGoToRoom, Button mbtnGoToSettings,Button mBtnOverview,ToggleButton mBtnStatus, ProgressBar mprogressBar, DigitalClock mClock)
{
this.mbtnGoToRoom = mbtnGoToRoom;
this.mBtnOverview = mbtnGoToSettings;
this.mbtnGoToSettings = mBtnOverview;
this.mBtnStatus = mBtnStatus;
this.mprogressBar = mprogressBar;
this.mClock = mClock;
}
public override void OnDismiss(IDialogInterface dialog)
{
base.OnDismiss(dialog);
mbtnGoToRoom.Visibility = ViewStates.Visible;
mBtnOverview.Visibility = ViewStates.Visible;
mbtnGoToSettings.Visibility = ViewStates.Visible;
mBtnStatus.Visibility = ViewStates.Visible;
mprogressBar.Visibility = ViewStates.Visible;
mClock.Visibility = ViewStates.Visible;
}