Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kivy.config import Config
- Config.set('graphics', 'width', '360')
- Config.set('graphics', 'height', '640')
- Config.set('graphics', 'resizable', '0')
- from kivy.app import App
- from kivy.lang import Builder
- from kivy.animation import Animation
- from kivy.core.window import Window
- from kivy.metrics import dp
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.button import Button
- from kivy.uix.widget import Widget
- from kivy.uix.behaviors import ButtonBehavior
- from kivy.uix.label import Label
- from kivy.properties import StringProperty
- from kivy.utils import platform
- Builder.load_string('''
- <ClickableArea>:
- canvas:
- Color:
- rgba: 0, 0, 0, 0
- Rectangle:
- pos: self.pos
- size: self.size
- <ContentButton>:
- orientation: 'horizontal'
- size_hint_y: None
- height: dp(50)
- spacing: dp(5)
- padding: dp(5)
- canvas.before:
- Color:
- rgba: 0.4, 0.4, 0.6, 1
- Rectangle:
- pos: self.pos
- size: self.size
- Label:
- id: main_label
- text: root.main_text
- size_hint_x: 0.8
- halign: 'left'
- valign: 'middle'
- text_size: self.width, None
- color: 1, 1, 1, 1
- font_size: dp(16)
- bold: True
- Button:
- id: info_button
- text: 'i'
- size_hint_x: 0.2
- size_hint_y: 1
- background_normal: ''
- background_color: 0.8, 0.8, 0.2, 1
- font_size: dp(14)
- bold: True
- on_press: root.on_info_press()
- <SearchAppUI>:
- orientation: 'vertical'
- spacing: dp(7)
- # Плашка результатов
- BoxLayout:
- id: result_panel
- size_hint_y: None
- height: 0
- opacity: 0
- canvas.before:
- Color:
- rgba: 0.2, 0.8, 0.4, 0.9
- Rectangle:
- pos: self.pos
- size: self.size
- Label:
- id: result_label
- text: ''
- color: 1, 1, 1, 1
- font_size: dp(18)
- bold: True
- halign: 'center'
- padding: [dp(10), dp(5)]
- # Основная область контента с кнопками
- ScrollView:
- id: main_scroll
- size_hint_y: 1
- do_scroll_y: True
- BoxLayout:
- id: content_box
- orientation: 'vertical'
- size_hint_y: None
- height: self.minimum_height
- spacing: dp(5)
- padding: [dp(10), dp(70)]
- # Поисковая панель (фиксированная внизу)
- BoxLayout:
- id: search_panel
- size_hint_y: None
- height: dp(60)
- padding: dp(10)
- spacing: dp(5)
- TextInput:
- id: search_input
- hint_text: 'Введите запрос...'
- multiline: False
- size_hint_x: 0.8
- padding: [dp(10), (self.height - self.line_height)/2]
- font_size: dp(16)
- on_focus: root.on_search_focus(*args)
- on_text_validate: root.do_search()
- Button:
- id: action_button
- text: 'Добавить'
- size_hint_x: 0.2
- font_size: dp(13)
- bold: True
- background_normal: ''
- background_color: (0.4, 0.6, 0.4, 1)
- on_press: root.toggle_result_panel()
- # Невидимая кликабельная область (над клавиатурой)
- ClickableArea:
- id: clickable_area
- size_hint_y: None
- height: 0
- on_press: root.hide_keyboard()
- ''')
- class ClickableArea(ButtonBehavior, Widget):
- pass
- class ContentButton(ButtonBehavior, BoxLayout):
- main_text = StringProperty('')
- info_text = StringProperty('')
- def __init__(self, main_text="", info_text="", **kwargs):
- super().__init__(**kwargs)
- self.main_text = main_text
- self.info_text = info_text
- def on_press(self):
- app = App.get_running_app()
- app.root.ids.result_label.text = f"Выбрано: {self.main_text}"
- if app.root.ids.result_panel.height == 0:
- app.root.show_result_panel()
- def on_info_press(self):
- app = App.get_running_app()
- app.root.ids.result_label.text = f"Инфо: {self.info_text}"
- if app.root.ids.result_panel.height == 0:
- app.root.show_result_panel()
- class SearchAppUI(BoxLayout):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- Window.bind(on_keyboard=self.on_keyboard)
- self.keyboard_height = dp(300)
- self.create_content_buttons()
- if platform == 'android':
- from android.runnable import run_on_ui_thread
- from jnius import autoclass
- self._activity = autoclass('org.kivy.android.PythonActivity').mActivity
- Window.bind(on_keyboard=self.on_keyboard)
- def on_keyboard(self, window, key, *args):
- if key == 27:
- if self.ids.search_input.focus:
- self.hide_keyboard()
- return True
- return False
- def create_content_buttons(self):
- content_box = self.ids.content_box
- for i in range(1, 21):
- btn = ContentButton(
- main_text=f'Элемент {i}',
- info_text=f'Доп. информация {i}'
- )
- content_box.add_widget(btn)
- def on_search_focus(self, instance, focused):
- if focused:
- self.ids.clickable_area.height = self.keyboard_height
- else:
- self.ids.clickable_area.height = 0
- def hide_keyboard(self):
- self.ids.search_input.focus = False
- def toggle_result_panel(self):
- if self.ids.result_panel.height == 0:
- query = self.ids.search_input.text.strip()
- if query:
- self.ids.result_label.text = f"Добавлено: {query}"
- else:
- self.ids.result_label.text = "Введите текст для добавления"
- self.show_result_panel()
- else:
- self.hide_result_panel()
- def show_result_panel(self):
- Animation(height=dp(50), opacity=1, duration=0.3).start(self.ids.result_panel)
- self.ids.action_button.text = "Убрать"
- self.ids.action_button.background_color = (0.8, 0.3, 0.3, 1)
- def hide_result_panel(self):
- Animation(height=0, opacity=0, duration=0.3).start(self.ids.result_panel)
- self.ids.action_button.text = "Добавить"
- self.ids.action_button.background_color = (0.4, 0.6, 0.4, 1)
- def do_search(self):
- query = self.ids.search_input.text.strip()
- if query:
- self.ids.result_label.text = f"Найдено: {query}"
- if self.ids.result_panel.height == 0:
- self.show_result_panel()
- class SearchApp(App):
- def build(self):
- Window.clearcolor = (0.4, 0.4, 0.4, 1)
- return SearchAppUI()
- if __name__ == '__main__':
- SearchApp().run()
Add Comment
Please, Sign In to add comment