[英]Back Pressed Xamarin Android
Hy Everybody, 大家好
I trying to use OnKeyDown in my app, but i trying to use inside a fragment, what happens... i have a Activity in this Activity, i have a Fragments that invoke others fragments and when a press back button, the app close... how can i implemente it? 我尝试在应用程序中使用OnKeyDown,但是尝试在片段内部使用,发生了什么...我在此活动中有一个Activity,我有一个Fragments会调用其他片段,并且当按下返回按钮时,应用程序关闭。 ..我该如何实施? Can someone give a example?
有人可以举个例子吗?
namespace Uer.Fragments
{
public class Login : Fragment
{
//Statement Objects
private TextView lblNewUser;
private LinearLayout lnlContainer;
private EditText edtEmail;
private EditText edtPassword;
private Button btnLogin;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
View view = inflater.Inflate(Resource.Layout.Login, container, false);
lnlContainer = view.FindViewById<LinearLayout>(Resource.Id.lnlContainer);
lblNewUser = view.FindViewById<TextView>(Resource.Id.lblNewUser);
edtEmail = view.FindViewById<EditText>(Resource.Id.edtEmail);
edtPassword = view.FindViewById<EditText>(Resource.Id.edtPassword);
btnLogin = view.FindViewById<Button>(Resource.Id.btnLogin);
lblNewUser.Click += LblNewUser_Click;
btnLogin.Click += BtnLogin_Click;
return view;
}
private void BtnLogin_Click(object sender, EventArgs e)
{
string email = edtEmail.Text;
string password = edtPassword.Text;
if (string.IsNullOrEmpty(email) || string.IsNullOrEmpty(password))
{
AlertDialog.Builder alert = new AlertDialog.Builder(Activity);
alert.SetTitle("Campos Vazios");
alert.SetMessage("Email ou Senha estao vazios");
alert.SetNeutralButton("OK", (senderAlert, args) =>
{
alert.Dispose();
});
alert.Create();
alert.Show();
} else if (!email.Equals("uer@uer.com") || !password.Equals("admin"))
{
AlertDialog.Builder alert = new AlertDialog.Builder(Activity);
alert.SetTitle("Email ou Senha Invalidos");
alert.SetMessage("Voce digitou email ou senha incorreto");
alert.SetNeutralButton("OK", (senderAlert, args) =>
{
alert.Dispose();
});
alert.Create();
alert.Show();
}
else
{
Intent intent = new Intent(Activity, typeof(MainActivity));
this.StartActivity(intent);
}
}
private void LblNewUser_Click(object sender, EventArgs e)
{
var transaction = Activity.SupportFragmentManager.BeginTransaction();
transaction.SetCustomAnimations(Resource.Animation.slide_in,
Resource.Animation.slide_out, Resource.Animation.slide_in,
Resource.Animation.slide_out);
transaction.Replace(Resource.Id.lnlContainer, new Register(), "Register");
transaction.Commit();
//Intent intent = new Intent(Activity, typeof(RegisterActivity));
//this.StartActivity(intent);
}
}
}
When you are transitioning between Fragments
, call addToBackStack()
: 在
Fragments
之间切换时,请调用addToBackStack()
:
FragmentTransaction FragmentTransaction tx = fragmentManager.beginTransation();
tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( "tag" ).commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.