【问题标题】:Custom keyboard clear button自定义键盘清除按钮
【发布时间】:2010-12-03 05:23:56
【问题描述】:

在我的应用程序中,我创建了一个自定义数字键盘。如何在连续点击清除按钮时删除文本字段的全部内容。有什么想法吗?

【问题讨论】:

    标签: iphone iphone-softkeyboard


    【解决方案1】:

    这很有趣。

    基本上我所做的就是编写一个方法来从 textField 的文本字符串中提取最后一个字符。

    我添加了另一个在 Button 的 touchDown 事件上触发的方法,它首先调用该方法以擦除最后一个字符,然后启动一个计时器以开始重复。因为重复之前的延迟(至少在本机键盘上)比重复延迟要长,所以我使用了两个计时器。第一个的重复选项设置为 NO。它调用一个方法来启动第二个计时器,该计时器重复调用擦除最后一个字符的方法。

    除了 touchDown 事件。我们还注册了 touchUpInside 事件。触发时,它会调用使当前计时器无效的方法。

        #import <UIKit/UIKit.h> 
    
        #define kBackSpaceRepeatDelay 0.1f
        #define kBackSpacePauseLengthBeforeRepeting 0.2f
    
        @interface clearontapAppDelegate : NSObject <UIApplicationDelegate> {
            UIWindow *window;
            NSTimer *repeatBackspaceTimer; 
            UITextField *textField;
        }
    
        @property (nonatomic, retain) IBOutlet UIWindow *window;
        @property (nonatomic, retain) NSTimer *repeatBackspaceTimer;
    
        @end
    
        @implementation clearontapAppDelegate
    
        @synthesize window, 
        @synthesize repeatBackspaceTimer;
    
        - (void)applicationDidFinishLaunching:(UIApplication *)application {    
            textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 300, 30)];
            textField.backgroundColor = [UIColor whiteColor];
            textField.text = @"hello world........";
    
            UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            button.frame = CGRectMake(10, 80, 300, 30);
            button.backgroundColor = [UIColor redColor];
            button.titleLabel.text = @"CLEAR";
    
            [button addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
            [button addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
            // Override point for customization after application launch
    
    
            [window addSubview:textField];
            [window addSubview:button];
            window.backgroundColor = [UIColor blueColor];
            [window makeKeyAndVisible];
        }
    
    
        -(void) eraseLastLetter:(id)sender {
            if (textField.text.length > 0) {
                textField.text = [textField.text substringToIndex:textField.text.length - 1];
            }
        }
    
        -(void) startRepeating:(id)sender {
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kBackSpaceRepeatDelay
                                                              target:self selector:@selector(eraseLastLetter:)
                                                            userInfo:nil repeats:YES];
            self.repeatBackspaceTimer = timer;
        }
    
        -(void) touchDown:(id)sender {
            [self eraseLastLetter:self];
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kBackSpacePauseLengthBeforeRepeting
                                                              target:self selector:@selector(startRepeating:)
                                                            userInfo:nil repeats:YES];
            self.repeatBackspaceTimer = timer;
        }
    
        -(void) touchUpInside:(id)sender {
            [self.repeatBackspaceTimer invalidate]; 
        }
    
        - (void)dealloc {
            [window release];
            [super dealloc];
        }
    
        @end
    

    【讨论】:

    • 以上代码将一键清除所有文本。我需要的正是 iPhone 键盘清除按钮的作用。即一键删除一个字符,连续点击清除所有内容。
    • 好的,现在我明白了。我为此编写了一个示例应用程序,并用上面的内容替换了我的帖子。
    • 非常好!!!非常感谢...现在我得到了我想要的东西。再次感谢!!!
    【解决方案2】:

    在 touchUpInside 上,删除单个字符。

    在touchDownRepeat上,删除整个单词或字段(我认为iPhone的delete是先一次删除一个单词)

    【讨论】:

    • Touchdown 重复将在单击按钮两次时删除字符。但我并没有离开我的按钮控制权。实际上它只是一个水龙头。
    猜你喜欢
    • 1970-01-01
    • 2014-10-27
    • 2020-02-16
    • 1970-01-01
    • 2018-09-17
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    相关资源
    最近更新 更多