2015-10-15 86 views
1

我试图在调用on_validate()方法后,如何将焦点放在我的TextInput小部件上。这样,点击RETURN后,我可以继续输入,而无需使用鼠标选择小部件。如何在基文的TextInput中设置光标位置

当我阅读TextInput文档时,所有游标选项似乎都假设游标已经在小部件中。

回答

0

在文档的显示,设定重点TextInput你这样做:

textinput = TextInput(focus=True) 

也许你可能会再次将焦点设置为True,在on_validate方法结束。 如何你恰好将取决于是否你从KV文件调用它或main.py

例如,在KV文件将是这个样子:

on_validate: mytextinput.focus = True 

而在main.py,它需要是这样的:

class MyTextInput(TextInput): 
    def __init__(self, **kwargs): 
     super(MyTextInput, self).__init__(kwargs) 

    def on_validate(self): 
     #do other stuff perhaps 
     self.focus = True 
+0

为什么downvote? – Totem

0

下面的片段展示了如何使用Kivy Clock.schedule_once的焦点重置中TextInput控件后按输入用户提交的文本^ h e在一行Textinput中输入。

commandTextInput = ObjectProperty() 

...

def refocusOnCommandTextInput(self): 
    #defining a delay of 0.1 sec ensure the 
    #refocus works in all situations. Leaving 
    #it empty (== next frame) does not work 
    #when pressing a button ! 
    Clock.schedule_once(self._refocusTextInput, 0.1)  


def _refocusTextInput(self, *args): 
    self.commandTextInput.focus = True 


#example of calling refocus function after 
#having handled new command entry 
def submitCommand(self): 

    # Get the student name from the TextInputs 
    commandStr = self.commandTextInput.text 
    #... some processing 
    self.refocusOnCommandTextInput() 

从.kv文件

TextInput: 
     id: command 
     background_color: 0,0,0,0 
     foreground_color: 1,1,1,1 
     focus: True 
     #ENTER triggers root.submitCommand() 
     multiline: False 
     on_text_validate: root.submitCommand()