Android-Xamarin:记住会话中的EditText

时间:2015-02-04 15:06:47

标签: visual-studio-2012 xamarin textview remember-me

我正在使用他的Xamarin插件在Visual Studio上开发一个Android应用程序。 我需要在此应用程序的所有会话中记住TextView上的用户和密码,即当用户关闭应用程序并重新打开时,此TextView必须记住字段上的文本。

谢谢你。

1 个答案:

答案 0 :(得分:0)

您必须使用ISharedPreferences。这是一个简单的例子,假设你有一个密码和一个按钮。:

 public class MainActivity : Activity
    {
        private ISharedPreferences _sharedPreferences;
        private EditText _editText;
        private Button _saveButton;
        private const string PasswordLabel = "SavedPassword";
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.test);

            this._editText = this.FindViewById<EditText>(Resource.Id.editText);
            this._saveButton = this.FindViewById<Button>(Resource.Id.button);
            this._sharedPreferences = PreferenceManager.GetDefaultSharedPreferences(this);
            this._editText.Text = _sharedPreferences.GetString(PasswordLabel, "");
            this._saveButton.Click += _saveButton_Click;

        }

        void _saveButton_Click(object sender, EventArgs e)
        {
            string password = this._editText.Text;
            ISharedPreferencesEditor editor = this._sharedPreferences.Edit();
            editor.PutString(PasswordLabel, password);
            editor.Commit();
        }
}
相关问题