点击输入字段时如何禁用文本突出显示?
How to disable text highlighting when tapping input field?
我知道之前有人问过这个问题,但 none 的解决方案似乎适用于移动设备。我发现 的解决方案在我的 PC 上运行良好,但不幸的是在我的平板电脑上运行不佳。我对解决方案发表了评论,看看他们是否可以提供帮助,但似乎他们已经一年多没有使用 SO 了。因此,我在这里试试运气,看看是否有人知道如何让他们的解决方案在移动设备上运行?
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TextFieldBehaviour : MonoBehaviour, ISelectHandler
{
private InputField inputField;
private bool isCaretPositionReset = false;
void Start()
{
inputField = gameObject.GetComponent<InputField>();
}
public void OnSelect(BaseEventData eventData)
{
StartCoroutine(disableHighlight());
}
IEnumerator disableHighlight()
{
Debug.Log("Selected!");
//Get original selection color
Color originalTextColor = inputField.selectionColor;
//Remove alpha
originalTextColor.a = 0f;
//Apply new selection color without alpha
inputField.selectionColor = originalTextColor;
//Wait one Frame(MUST DO THIS!)
yield return null;
//Change the caret pos to the end of the text
inputField.caretPosition = inputField.text.Length;
//Return alpha
originalTextColor.a = 1f;
//Apply new selection color with alpha
inputField.selectionColor = originalTextColor;
if (inputField.touchScreenKeyboard.canSetSelection)
inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);
}
}
如果您正在撰写有关本机屏幕键盘的文章,您可以尝试 this:
if (inputField.touchScreenKeyboard != null && inputField.touchScreenKeyboard.active && inputField.touchScreenKeyboard.canSetSelection)
inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);
RangeInt的参数为选区的起始位置和长度;
请记住,并非所有平台都支持此功能。有关 TouchScreenKeyboard 的所有功能,请参阅文档 here and here。
我知道之前有人问过这个问题,但 none 的解决方案似乎适用于移动设备。我发现
这是我的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class TextFieldBehaviour : MonoBehaviour, ISelectHandler
{
private InputField inputField;
private bool isCaretPositionReset = false;
void Start()
{
inputField = gameObject.GetComponent<InputField>();
}
public void OnSelect(BaseEventData eventData)
{
StartCoroutine(disableHighlight());
}
IEnumerator disableHighlight()
{
Debug.Log("Selected!");
//Get original selection color
Color originalTextColor = inputField.selectionColor;
//Remove alpha
originalTextColor.a = 0f;
//Apply new selection color without alpha
inputField.selectionColor = originalTextColor;
//Wait one Frame(MUST DO THIS!)
yield return null;
//Change the caret pos to the end of the text
inputField.caretPosition = inputField.text.Length;
//Return alpha
originalTextColor.a = 1f;
//Apply new selection color with alpha
inputField.selectionColor = originalTextColor;
if (inputField.touchScreenKeyboard.canSetSelection)
inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);
}
}
如果您正在撰写有关本机屏幕键盘的文章,您可以尝试 this:
if (inputField.touchScreenKeyboard != null && inputField.touchScreenKeyboard.active && inputField.touchScreenKeyboard.canSetSelection)
inputField.touchScreenKeyboard.selection = new RangeInt(0, 0);
RangeInt的参数为选区的起始位置和长度;
请记住,并非所有平台都支持此功能。有关 TouchScreenKeyboard 的所有功能,请参阅文档 here and here。