将光标移动到MDTextField中字符串的最终位置



我正在尝试为我使用KivyMD开发的应用程序上的MDTextField提供日期格式。此类字段的格式为"dd/mm/yyyy"。

我想做的是,一旦写入前两个数字,就会自动写入"/",光标将跳到"/"右侧的最后一个位置(例如"21/"(。以同样的方式,在第一个"/"之后写入其他2个数字后,将写入第二个"/",光标将再次移动到末尾(例如"21/09/"(。

我已经设法使两个"/"都出现,然而,这是我无法将光标放置在所需位置的光标。我的代码如下:

def apply_date_format(self):
# delete '/' if len is equal or less than 2 and final character is /  
if len(self.ids.viajeInicio.text) =< 2 and (self.ids.viajeInicio.text).endswith('/'):
self.ids.viajeInicio.text = (self.ids.viajeInicio.text[:-1])
# first '/'
elif len(self.ids.viajeInicio.text) == 2 and (self.ids.viajeInicio.text).isnumeric():
self.ids.viajeInicio.text= self.ids.viajeInicio.text + "/"
# second '/'
elif len(self.ids.viajeInicio.text) == 5 and (self.ids.viajeInicio.text[3:5]).isnumeric():
self.ids.viajeInicio.text= self.ids.viajeInicio.text + "/"
# delete last '/' if len is <= 5 and last character is '/'
elif len(self.ids.viajeInicio.text) > 3 and len(self.ids.viajeInicio.text) <= 5 
and (self.ids.viajeInicio.text).endswith('/'):
self.ids.viajeInicio.text = (self.ids.viajeInicio.text[:-1])

MDTextField的id为viajeInicio,函数apply_date_format在on_text事件上被调用。代码如下:

MDTextField:
id: viajeInicio
hint_text: 'Ingresar Fecha de Inicio del Viaje'
pos_hint: {"x":0, "top":1}
helper_text: 'Formato de fecha: dd/mm/aaaa'
helper_text_mode: 'on_focus'
required: True
on_text:
root.apply_date_format()

在写入"/"之后,如何将光标位置移动到字符串的末尾。此外,有没有更好的方法来完成期望的任务?

提前感谢

我认为只扩展MDTextField类并超越它的insert_text()方法更简单。类似这样的东西:

class DateMDTextField(MDTextField):
def insert_text(self, the_text, from_undo=False):
if the_text == '/':
# do not allow typed in '/'
return
cc, cr = self.cursor
cur_text = self._lines[cr] + the_text  # existing text plus the to be inserted the_text
cur_len = len(cur_text)
# new_text will be inserted. The default is to just use the_text
new_text = the_text
# delete '/' if len is equal or less than 2 and final character is /
if cur_len <= 2 and cur_text.endswith('/'):
new_text = new_text[:-1]
# first '/'
elif cur_len == 2 and cur_text.isnumeric():
new_text += '/'
# second '/'
elif cur_len == 5 and cur_text[3:5].isnumeric():
new_text += '/'
# delete last '/' if len is <= 5 and last character is '/'
elif cur_len > 3 and cur_len <= 5 and cur_text.endswith('/'):
new_text = new_text[:-1]
# do not allow extra characters
elif cur_len > 10:
return
# call the insert_text() of the MDTextField with the possibly modified text
super(DateMDTextField, self).insert_text(new_text, from_undo=from_undo)

上面的代码使用了您的逻辑(添加了一些小的内容(,由于它最终只调用了MDTextFieldinsert_text(),所以所有的光标移动都由您处理。

所以你可以直接替换:

MDTextField:
id: viajeInicio
hint_text: 'Ingresar Fecha de Inicio del Viaje'
pos_hint: {"x":0, "top":1}
helper_text: 'Formato de fecha: dd/mm/aaaa'
helper_text_mode: 'on_focus'
required: True
on_text:
root.apply_date_format()

带有:

DateMDTextField:
id: viajeInicio
hint_text: 'Ingresar Fecha de Inicio del Viaje'
pos_hint: {"x":0, "top":1}
helper_text: 'Formato de fecha: dd/mm/aaaa'
helper_text_mode: 'on_focus'
required: True

在你的"千伏"。您不需要显式调用insert_text()方法,因为它是由基类TextInput自动调用的。而且您也不需要on_text条目。

并且您不再需要apply_date_format()方法。

最新更新