Guest User

Untitled

a guest
Nov 20th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.75 KB | None | 0 0
  1. namespace FloatingEntryApp.Droid.Renderers
  2. {
  3. public class MyEntryRenderer : Xamarin.Forms.Platform.Android.AppCompat.ViewRenderer<FloatingEntry, TextInputLayout>
  4. {
  5. private EditText _defaultEditTextForValues;
  6. private bool _preventTextLoop;
  7. private EditText EditText => Control.EditText;
  8.  
  9. protected override void OnElementChanged(ElementChangedEventArgs<FloatingEntry> e)
  10. {
  11. base.OnElementChanged(e);
  12.  
  13. if (e.OldElement != null)
  14. {
  15. EditText.FocusChange -= ControlOnFocusChange;
  16. Control.EditText.KeyPress -= EditTextOnKeyPress;
  17. Control.EditText.TextChanged -= EditTextOnTextChanged;
  18. }
  19.  
  20. if (e.NewElement != null)
  21. {
  22. var ctrl = CreateNativeControl();
  23. SetNativeControl(ctrl);
  24. _defaultEditTextForValues = new EditText(Context);
  25.  
  26. Focusable = true;
  27. Control.HintEnabled = true;
  28. Control.HintAnimationEnabled = true;
  29. EditText.ShowSoftInputOnFocus = true;
  30.  
  31. EditText.FocusChange += ControlOnFocusChange;
  32. EditText.ImeOptions = ImeAction.Done;
  33.  
  34. SetText();
  35. SetHintText();
  36. SetTextColor();
  37. SetBackgroundColor();
  38. SetHintColor();
  39. SetIsPassword();
  40. SetKeyboard();
  41. SetInputType();
  42.  
  43. var nativeEditText = Control.EditText;
  44. nativeEditText.SetTextSize(ComplexUnitType.Pt, 9);
  45.  
  46. Control.EditText.TextChanged += EditTextOnTextChanged;
  47. Control.EditText.KeyPress += EditTextOnKeyPress;
  48. }
  49. }
  50.  
  51. private void ControlOnFocusChange(object sender, FocusChangeEventArgs args)
  52. {
  53. if (args.HasFocus)
  54. {
  55. var manager = (InputMethodManager)Application.Context.GetSystemService(Context.InputMethodService);
  56.  
  57. EditText.PostDelayed(() =>
  58. {
  59. EditText.RequestFocus();
  60. manager.ShowSoftInput(EditText, 0);
  61. },
  62. 100);
  63. }
  64.  
  65. var isFocusedPropertyKey = Element.GetInternalField<BindablePropertyKey>("IsFocusedPropertyKey");
  66. ((IElementController)Element).SetValueFromRenderer(isFocusedPropertyKey, args.HasFocus);
  67. }
  68.  
  69. protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
  70. {
  71. base.OnElementPropertyChanged(sender, e);
  72.  
  73. if (e.PropertyName == FloatingEntry.PlaceholderProperty.PropertyName)
  74. {
  75. SetHintText();
  76. }
  77.  
  78. if (e.PropertyName == FloatingEntry.TextColorProperty.PropertyName)
  79. {
  80. SetTextColor();
  81. }
  82.  
  83. if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
  84. {
  85. SetBackgroundColor();
  86. }
  87.  
  88. if (e.PropertyName == FloatingEntry.IsPasswordProperty.PropertyName)
  89. {
  90. SetIsPassword();
  91. }
  92.  
  93. if (e.PropertyName == FloatingEntry.TextProperty.PropertyName)
  94. {
  95. SetText();
  96. }
  97.  
  98. if (e.PropertyName == FloatingEntry.PlaceholderColorProperty.PropertyName)
  99. {
  100. SetHintColor();
  101. }
  102.  
  103. if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
  104. {
  105. SetKeyboard();
  106. }
  107.  
  108. if (e.PropertyName == InputView.KeyboardProperty.PropertyName)
  109. {
  110. SetInputType();
  111. }
  112. }
  113.  
  114. private void ElementOnHideKeyboard(object sender, EventArgs eventArgs)
  115. {
  116. var manager = (InputMethodManager)Android.App.Application.Context.GetSystemService(Context.InputMethodService);
  117. manager.HideSoftInputFromWindow(Control.EditText.WindowToken, 0);
  118. }
  119.  
  120. private void SetIsPassword()
  121. {
  122. Control.EditText.InputType = Element.IsPassword
  123. ? InputTypes.TextVariationPassword | InputTypes.ClassText
  124. : Control.EditText.InputType;
  125. }
  126.  
  127. private void SetBackgroundColor()
  128. {
  129. Control.SetBackgroundColor(Element.BackgroundColor.ToAndroid());
  130. }
  131.  
  132. private void SetHintText()
  133. {
  134. Control.Hint = Element.Placeholder;
  135. }
  136.  
  137. private void SetHintColor()
  138. {
  139. if (Element.PlaceholderColor == Color.Default)
  140. {
  141. Control.EditText.SetHintTextColor(_defaultEditTextForValues.HintTextColors);
  142. }
  143. else
  144. {
  145. Control.EditText.SetHintTextColor(Element.PlaceholderColor.ToAndroid());
  146. }
  147. }
  148.  
  149. private void SetTextColor()
  150. {
  151. if (Element.TextColor == Color.Default)
  152. {
  153. Control.EditText.SetTextColor(_defaultEditTextForValues.TextColors);
  154. }
  155. else
  156. {
  157. Control.EditText.SetTextColor(Element.TextColor.ToAndroid());
  158. }
  159. }
  160.  
  161. private void SetKeyboard()
  162. {
  163. Control.EditText.InputType = Element.Keyboard.ToNative();
  164. }
  165.  
  166. protected override TextInputLayout CreateNativeControl()
  167. {
  168. var layout = (TextInputLayout)LayoutInflater.From(Context).Inflate(Resource.Layout.TextInputLayout, null);
  169. var inner = layout.FindViewById(Resource.Id.textInputEdit);
  170. if (!string.IsNullOrWhiteSpace(Element.AutomationId))
  171. {
  172. inner.ContentDescription = Element.AutomationId;
  173. }
  174. return layout;
  175.  
  176. }
  177.  
  178. private void EditTextOnKeyPress(object sender, KeyEventArgs args)
  179. {
  180. args.Handled = args.KeyCode == Keycode.Enter;
  181. if (args.KeyCode == Keycode.Enter && args.Event.Action == KeyEventActions.Up)
  182. {
  183. Control.ClearFocus();
  184. HideKeyboard();
  185. ((IEntryController)Element).SendCompleted();
  186. }
  187. }
  188.  
  189. private void EditTextOnTextChanged(object sender, Android.Text.TextChangedEventArgs args)
  190. {
  191. var selection = Control.EditText.SelectionStart;
  192. if (!_preventTextLoop)
  193. {
  194. Element.Text = args.Text.ToString();
  195. }
  196. if (Element == null || Element.Text == null) return;
  197.  
  198. var index = selection > Element.Text.Length ? Element.Text.Length : selection;
  199. Control.EditText.SetSelection(index);
  200. }
  201.  
  202. private void SetText()
  203. {
  204. _preventTextLoop = true;
  205. if (Control.EditText.Text != Element.Text)
  206. {
  207. Control.EditText.Text = Element.Text;
  208. if (EditText.IsFocused)
  209. EditText.SetSelection(EditText.Text.Length);
  210. }
  211. _preventTextLoop = false;
  212. }
  213.  
  214. protected override void Dispose(bool disposing)
  215. {
  216. Control.EditText.KeyPress -= EditTextOnKeyPress;
  217. Control.EditText.TextChanged -= EditTextOnTextChanged;
  218. base.Dispose(disposing);
  219. }
  220.  
  221. protected void HideKeyboard()
  222. {
  223. var manager = (InputMethodManager)Application.Context.GetSystemService(Context.InputMethodService);
  224. manager.HideSoftInputFromWindow(EditText.WindowToken, 0);
  225. }
  226.  
  227. private void SetInputType()
  228. {
  229. EditText.InputType = Element.Keyboard.ToInputType();
  230. if (Element.IsPassword && ((EditText.InputType & InputTypes.ClassText) == InputTypes.ClassText))
  231. {
  232. EditText.InputType = EditText.InputType | InputTypes.TextVariationPassword;
  233. }
  234. if (Element.IsPassword && ((EditText.InputType & InputTypes.ClassNumber) == InputTypes.ClassNumber))
  235. {
  236. EditText.InputType = EditText.InputType | InputTypes.NumberVariationPassword;
  237. }
  238. }
  239. }
  240. }
  241.  
  242. public class LoginPage : ContentPage
  243. {
  244. FloatingEntry EntryFour, EntryFive, EntryOne, EntryTwo, EntryThree;
  245. Button button;
  246. StackLayout detailStack;
  247.  
  248. public LoginPage()
  249. {
  250. button = new Button
  251. {
  252. Text = "Button",
  253. FontSize = 20,
  254. FontAttributes = FontAttributes.Bold,
  255. TextColor = Color.RoyalBlue,
  256. BackgroundColor = Color.White,
  257. HeightRequest = 50,
  258. };
  259.  
  260. StackLayout mainStack = new StackLayout();
  261. mainStack.BackgroundColor = Color.White;
  262. mainStack.Spacing = 5;
  263.  
  264. StackLayout labelStack = new StackLayout();
  265. labelStack.BackgroundColor = Color.Gray;
  266. labelStack.HeightRequest = 100;
  267.  
  268. Label loginHeaderLabel = new Label();
  269. loginHeaderLabel.Text = "Heading";
  270. loginHeaderLabel.FontSize = 25;
  271. loginHeaderLabel.HorizontalOptions = LayoutOptions.FillAndExpand;
  272. loginHeaderLabel.VerticalOptions = LayoutOptions.EndAndExpand;
  273. loginHeaderLabel.HorizontalTextAlignment = TextAlignment.Start;
  274. loginHeaderLabel.VerticalTextAlignment = TextAlignment.End;
  275. loginHeaderLabel.TextColor = Color.White;
  276. loginHeaderLabel.FontAttributes = FontAttributes.Bold;
  277. loginHeaderLabel.Margin = new Thickness(30, 5, 5, 10);
  278.  
  279.  
  280. labelStack.Children.Add(loginHeaderLabel);
  281.  
  282. ScrollView scrollView = new ScrollView();
  283. scrollView.BackgroundColor = Color.FromHex("#FFFFFF");
  284.  
  285. detailStack = new StackLayout();
  286. detailStack.Padding = new Thickness(20, 10, 20, 20);
  287. detailStack.Spacing = 5;
  288.  
  289. EntryOne = new FloatingEntry();
  290. EntryOne.Placeholder = "Entry One";
  291. EntryOne.PlaceholderColor = Color.Green;
  292. EntryOne.TextColor = Color.Black;
  293. EntryOne.HorizontalTextAlignment = TextAlignment.Start;
  294. EntryOne.Keyboard = Keyboard.Url;
  295.  
  296. EntryTwo = new FloatingEntry();
  297. EntryTwo.Placeholder = "Entry Two";
  298. EntryTwo.PlaceholderColor = Color.Green;
  299. EntryTwo.TextColor = Color.Black;
  300.  
  301. EntryThree = new FloatingEntry();
  302. EntryThree.Placeholder = "Entry Three";
  303. EntryThree.PlaceholderColor = Color.Green;
  304. EntryThree.TextColor = Color.Black;
  305. EntryThree.HorizontalTextAlignment = TextAlignment.Start;
  306.  
  307. EntryFour = new FloatingEntry();
  308. EntryFour.Placeholder = "Entry Four";
  309. EntryFour.PlaceholderColor = Color.Green;
  310. EntryFour.TextColor = Color.Black;
  311. EntryFour.HorizontalTextAlignment = TextAlignment.Start;
  312.  
  313. EntryFive = new FloatingEntry();
  314. EntryFive.Placeholder = "Entry Five";
  315. EntryFive.PlaceholderColor = Color.Green;
  316. EntryFive.TextColor = Color.Black;
  317. EntryFive.HorizontalTextAlignment = TextAlignment.Start;
  318. EntryFive.IsPassword = true;
  319.  
  320. detailStack.Children.Add(EntryOne);
  321. detailStack.Children.Add(EntryTwo);
  322. detailStack.Children.Add(EntryThree);
  323. detailStack.Children.Add(EntryFour);
  324. detailStack.Children.Add(EntryFive);
  325. detailStack.Children.Add(button);
  326.  
  327. scrollView.Content = detailStack;
  328.  
  329. mainStack.Children.Add(labelStack);
  330. mainStack.Children.Add(scrollView);
  331.  
  332. Content = mainStack;
  333.  
  334. }
  335.  
  336. }
Add Comment
Please, Sign In to add comment