【问题标题】:Capture iOS UITextField clear event with Xamarin使用 Xamarin 捕获 iOS UITextField 清除事件
【发布时间】:2016-03-28 13:16:31
【问题描述】:

我想在按下 textview 的 (x) 时执行一些操作(重置搜索)。

类似这样的东西:iPhone perform action when UITextField / UISearchBar's clear button is pressed 但我不知道如何使用 Xamarin 进行操作

我有这个:

tbkSearchOrder.ShouldReturn += (textField) => { 
   ComboViewCtl.OnOrdersFiltered (this, tbkSearchOrder.Text);
   return true; 
};

它响应键盘上按下的“搜索”按钮。 UITextField 有几个与此类似的方法,但它们是方法,而不是事件、动作或委托。理想情况下,我会避免为该文本字段编写一个大的 Delegate 类。

【问题讨论】:

    标签: ios xamarin


    【解决方案1】:

    它们是方法,因为它们实际上返回一个值。 C# 事件只是接收对象发送的事件的一种方式。

    当 UITextField 启动(显示键盘并且对象准备好管理输入事件)时,将发送 Started 事件。你可以用这个来清除 UITextField

    UITextField textField = new UITextField();
    textField.Started += delegate {
         textField.Text = null;
    };
    

    用于处理清除按钮上的点击(遗憾的是,这不是一个事件,但如果委托返回 true,TextField 无论如何都会被清除):

    UITextField textField = new UITextField();
    textField.ShouldClear += delegate {
        // Insert the handling here
        return true;
    };
    

    【讨论】:

    • 我不需要清除文本字段,我需要在清除文本字段时重置搜索过滤器
    • +1 谢谢!显然在 c# 绑定中“ShouldClear”是一个属性(委托)而不是一个事件,所以我在查看列表时错过了它
    【解决方案2】:
                this.sampleTextField1.ShouldReturn += (textField) => {
                    sampleTextField2.BecomeFirstResponder ();
                    textField.ResignFirstResponder ();
                    return true;
                };
    
                this.sampleTextField1.ShouldBeginEditing += (textField) => {
                    //Write here
                    return true;
                };
    
                this.sampleTextField1.ShouldEndEditing += (textField) => {
                    //Write here
                    return true;
                };
                this.sampleTextField1.ShouldClear += (textField) => {
                    //Write here
                    return true;
                };
    
                this.sampleTextField1.ShouldChangeCharacters = (UITextField txt, NSRange range, string sampleTxt) => {
                    var newLength = txt.Text.Length + sampleTxt.Length - range.Length;
                    return newLength <= 9;
    
                };
    

    【讨论】:

    • 我看不出这个答案对我已经接受的答案有何改进:更多的代码,不相关的代码(.Alpha = 1.0f),它显示了代表但没有告诉你在哪里处理所需的事件。很差的答案
    • @Sten :好的做法是将委托添加到 viewDidLoad ( this.enableUITextFieldDelegateMethods (); 。我把它做成了一个单独的方法。)。请看@github.com/alvinreuben/BasicUI-Xamarin
    • 还有太多不相关的垃圾内容无法让读者看到此声明
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 2019-04-03
    • 1970-01-01
    • 2014-11-09
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多