gpt4 book ai didi

c# - 将字符串从 Activity 传递到 DialogFragment

转载 作者:太空狗 更新时间:2023-10-29 16:15:19 26 4
gpt4 key购买 nike

在我的 Activity 中我有这个:

公共(public)类 MainActivity : Activity { 相对布局 mRelativeLayout; 按钮 mButton; 私有(private)按钮 mBtnSignUp;

    protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

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

mRelativeLayout = FindViewById<RelativeLayout>(Resource.Id.mainView);
mButton = FindViewById<Button>(Resource.Id.btnLogin);

mButton.Click += mButton_Click;
mRelativeLayout.Click += mRelativeLayout_Click;

mBtnSignUp = FindViewById<Button> (Resource.Id.button1);
mBtnSignUp.Click += (object sender, EventArgs e) =>
{
//Pull up dialog
FragmentTransaction transaction = FragmentManager.BeginTransaction();
dialog_SignUp signUpDialog = new dialog_SignUp();
signUpDialog.Show(transaction, "dialog fragment");

signUpDialog.mOnSignUpComplete += signUpDialog_mOnSignUpComplete;
};
void signUpDialog_mOnSignUpComplete (object sender, OnSignUpEventArgs e)
{

Thread thread = new Thread (ActLikeARequest);
thread.Start ();


}
private void ActLikeARequest()
{

Thread.Sleep (3000);
}

在对话框中我有:

public class OnSignUpEventArgs : EventArgs
{
private string mFirstName;
private string mEmail;
private string mPassword;

public string FirstName
{
get{ return mFirstName; }
set{ mFirstName=value;}

}
public string Email
{
get{ return mEmail; }
set{ mEmail=value;}

}
public string Password
{
get{ return mPassword; }
set{ mPassword=value;}

}

public OnSignUpEventArgs (string firstName, string email, string password) : base()
{
FirstName = firstName;
Email = email;
Password = password;
}

}

class dialog_SignUp:DialogFragment
{
private Button mBtnSignUp;
private EditText mFirstName;
private EditText mEmail;
private EditText mPassword;

public event EventHandler<OnSignUpEventArgs> mOnSignUpComplete;


public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);

var view = inflater.Inflate (Resource.Layout.dialog_sign_up, container, false);

mBtnSignUp = view.FindViewById<Button> (Resource.Id.btnDialogEmail);
mFirstName = view.FindViewById<EditText> (Resource.Id.txtFirstName);
mEmail = view.FindViewById<EditText> (Resource.Id.txtEmail);
mPassword = view.FindViewById<EditText> (Resource.Id.txtPassword);

mBtnSignUp.Click += mBtnSignUp_Click;



return view;
}

void mBtnSignUp_Click (object sender, EventArgs e)
{

//User has clicked the sign up button

mOnSignUpComplete.Invoke (this, new OnSignUpEventArgs(mFirstName.Text, mEmail.Text, mPassword.Text));
this.Dismiss ();

}
public override void OnActivityCreated (Bundle savedInstanceState)
{
Dialog.Window.RequestFeature (WindowFeatures.NoTitle); //Sets the title bar to invisible
base.OnActivityCreated (savedInstanceState);
Dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation; //Set the animation
}
}

问题是,如何将 Activity 中的数据(字符串)传递到对话框中,以便在调用它时,TextView 获取我传递的数据?

是不是要在 Activity 中添加一些东西,但是什么?

最佳答案

我认为您需要能够在 dialog_signup 中接受参数。

class dialog_SignUp:DialogFragment
{
private Button mBtnSignUp;
private EditText mFirstName;
private EditText mEmail;
private EditText mPassword;
String parameter="";
public event EventHandler<OnSignUpEventArgs> mOnSignUpComplete;

public dialog_SignUp(String parameterIn){
parameter=parameterIn;
}

public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView (inflater, container, savedInstanceState);

var view = inflater.Inflate (Resource.Layout.dialog_sign_up, container, false);

mBtnSignUp = view.FindViewById<Button> (Resource.Id.btnDialogEmail);
mFirstName = view.FindViewById<EditText> (Resource.Id.txtFirstName);
mFirstName.setText(parameter);
mEmail = view.FindViewById<EditText> (Resource.Id.txtEmail);
mPassword = view.FindViewById<EditText> (Resource.Id.txtPassword);

mBtnSignUp.Click += mBtnSignUp_Click;



return view;
}

然后将 dialog_SignUp signUpDialog = new dialog_SignUp(); 替换为 dialog_SignUp signUpDialog = new dialog_SignUp(stringToSend);

关于c# - 将字符串从 Activity 传递到 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29899902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com