Ibr*_*him 4 c# xaml onfocus xamarin.forms
如何在Entry,Editor,Label中自动选择焦点上的所有文本?使用跨平台.
Quantity.IsFocused = true;
Run Code Online (Sandbox Code Playgroud)
没工作:(
SAI*_* KP 11
1.添加焦点事件.Cs
protected void Txt_Focussed(object sender, FocusEventArgs e)
{
txt.CursorPosition = 0;
txt.SelectionLength = txt.Text.Length;
}
Run Code Online (Sandbox Code Playgroud)
设置焦点
protected override void OnAppearing()
{
base.OnAppearing();
txt.Focus();
}
Run Code Online (Sandbox Code Playgroud)
XAML 代码
<Entry x:Name="txt" Text="155134343" Focused="Txt_Focussed" />
Run Code Online (Sandbox Code Playgroud)
如其他答案中所述,如果您使用的是 Xamarin Forms 4.2+,则可以使用 CursorPosition 和 SelectionLength 属性。但是,您需要确保在主线程上调用它,否则它将无法工作:
XAML
<Entry x:Name="MyEntry" Focused="MyEntry_Focused" />
Run Code Online (Sandbox Code Playgroud)
C#
private void MyEntry_Focused(object sender, FocusEventArgs e)
{
Dispatcher.BeginInvokeOnMainThread(() =>
{
MyEntry.CursorPosition = 0;
MyEntry.SelectionLength = MyEntry.Text != null ? MyEntry.Text.Length : 0
});
}
Run Code Online (Sandbox Code Playgroud)
在MainActivity中添加
public class MyEntryRenderer : EntryRenderer
{
protected override void OnElementChanged (ElementChangedEventArgs<Entry> e)
{
base.OnElementChanged (e);
if (e.OldElement == null) {
var nativeEditText = (global::Android.Widget.EditText)Control;
nativeEditText.SetSelectAllOnFocus (true);
}
}
}
Run Code Online (Sandbox Code Playgroud)
并添加到顶部:
[assembly: ExportRenderer (typeof (Entry), typeof (MyEntryRenderer))]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5375 次 |
最近记录: |