gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Мне нужно чтобы после того как введем часы каретка в строке ввода переместилась в самый…
- User
Мне нужно чтобы после того как введем часы каретка в строке ввода переместилась в самый угол using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System.Text.RegularExpressions;
public class TimeInputField : MonoBehaviour { [SerializeField] private TMP_InputField inputField;
private int hours; private int minutes; private string newValue; private void Start() { inputField.onValueChanged.AddListener(OnValueChanged); } private void OnValueChanged(string value) { // Check if the input contains only digits if (!Regex.IsMatch(value, @"^\d *$")) { // Remove any non-digit characters from the input value = Regex.Replace(value, @"\D", ""); newValue = value; //return; } else { newValue = value; } // Check the input length for the first two characters if (newValue.Length == 2) { // Try parsing the hours if (int.TryParse(newValue, out hours)) { // Check if hours is not greater than 24 if (hours > 24) { hours = 24; } // Update the input field with the formatted hours inputField.text = string.Format("{0:D2} (ч) :", hours); return; } } // Check the input length for the next two characters if (newValue.Length == 4) { // Extract minutes string minutesStr = newValue.Substring(2, 2); // Try parsing the minutes if (int.TryParse(minutesStr, out minutes)) { // Check if minutes is greater than 60 if (minutes > 60) { // Calculate the exceeding minutes and add them to hours int exceedingMinutes = minutes - 60; int existingHours = 0; // Extract existing hours from the input field if (inputField.text.Length >= 9) { string existingHoursStr = inputField.text.Substring(0, 2); int.TryParse(existingHoursStr, out existingHours); } // Add the exceeding minutes to the existing hours int newHours = existingHours + 1; int newMinutes = exceedingMinutes; // Check if new hours is not greater than 24 if (newHours > 24) { newHours = 24; } // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", newHours, newMinutes); inputField.text = formattedValue; return; } else { // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", hours, minutes); inputField.text = formattedValue; return; } } } }}
- Assistant
Для перемещения каретки в самый угол после ввода часов в строке ввода, вы можете добавить следующий код в метод OnValueChanged:
// Move caret to the end of the input field inputField.caretPosition = inputField.text.Length;Полный код класса TimeInputField после добавления этого кода будет выглядеть так:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using System.Text.RegularExpressions; public class TimeInputField : MonoBehaviour { [SerializeField] private TMP_InputField inputField; private int hours; private int minutes; private string newValue; private void Start() { inputField.onValueChanged.AddListener(OnValueChanged); } private void OnValueChanged(string value) { // Check if the input contains only digits if (!Regex.IsMatch(value, @"^\d *$")) { // Remove any non-digit characters from the input value = Regex.Replace(value, @"\D", ""); newValue = value; //return; } else { newValue = value; } // Check the input length for the first two characters if (newValue.Length == 2) { // Try parsing the hours if (int.TryParse(newValue, out hours)) { // Check if hours is not greater than 24 if (hours > 24) { hours = 24; } // Update the input field with the formatted hours inputField.text = string.Format("{0:D2} (ч) :", hours); // Move caret to the end of the input field inputField.caretPosition = inputField.text.Length; return; } } // Check the input length for the next two characters if (newValue.Length == 4) { // Extract minutes string minutesStr = newValue.Substring(2, 2); // Try parsing the minutes if (int.TryParse(minutesStr, out minutes)) { // Check if minutes is greater than 60 if (minutes > 60) { // Calculate the exceeding minutes and add them to hours int exceedingMinutes = minutes - 60; int existingHours = 0; // Extract existing hours from the input field if (inputField.text.Length >= 9) { string existingHoursStr = inputField.text.Substring(0, 2); int.TryParse(existingHoursStr, out existingHours); } // Add the exceeding minutes to the existing hours int newHours = existingHours + 1; int newMinutes = exceedingMinutes; // Check if new hours is not greater than 24 if (newHours > 24) { newHours = 24; } // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", newHours, newMinutes); inputField.text = formattedValue; // Move caret to the end of the input field inputField.caretPosition = inputField.text.Length; return; } else { // Apply the desired format string formattedValue = string.Format("{0:D2} (ч) : {1:D2} (мин)", hours, minutes); inputField.text = formattedValue; // Move caret to the end of the input field inputField.caretPosition = inputField.text.Length; return; } } } } }Теперь каретка будет перемещаться в самый угол после ввода часов.