This commit is contained in:
polwex 2024-10-15 07:15:41 +07:00
parent 94933f6d47
commit 8120191874
2 changed files with 39 additions and 15 deletions

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -11,16 +11,23 @@ import com.google.gson.Gson
import android.widget.Button import android.widget.Button
import android.widget.HorizontalScrollView import android.widget.HorizontalScrollView
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView
import kotlin.properties.Delegates
class MyKeyboardService : InputMethodService() { class MyKeyboardService : InputMethodService() {
private val coroutineScope = CoroutineScope(Dispatchers.Main + Job()) private val coroutineScope = CoroutineScope(Dispatchers.Main + Job())
private var currentInput = StringBuilder() private var currentInput: String by Delegates.observable("") { _, _, newValue ->
updateCurrentInputDisplay(newValue)
}
private val gson = Gson() private val gson = Gson()
private lateinit var suggestionsContainer: LinearLayout private lateinit var suggestionsContainer: LinearLayout
private var isShiftActive = false private lateinit var currentInputDisplay: TextView
private var isShiftActive = false
private fun updateCurrentInputDisplay(text: String) {
currentInputDisplay.text = text
}
override fun onCreateInputView(): View { override fun onCreateInputView(): View {
@ -41,10 +48,22 @@ class MyKeyboardService : InputMethodService() {
LinearLayout.LayoutParams.WRAP_CONTENT LinearLayout.LayoutParams.WRAP_CONTENT
) )
} }
currentInputDisplay = TextView(this).apply{
layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
).apply {
setMargins(10, 10, 10, 10)
}
setTextColor(Color.BLACK)
setBackgroundColor(Color.LTGRAY)
textSize = 18f
setPadding(10, 10, 10, 10)
}
inputView.addView(currentInputDisplay)
inputView.addView(suggestionsContainer, 0) scrollView.addView(suggestionsContainer)
inputView.addView(scrollView, 0) inputView.addView(scrollView)
setupKeyboardKeys(inputView) setupKeyboardKeys(inputView)
return inputView return inputView
} }
@ -104,7 +123,7 @@ class MyKeyboardService : InputMethodService() {
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT LinearLayout.LayoutParams.WRAP_CONTENT
) )
setOnClickListener { onKeyPressed(" ") } setOnClickListener { onSpace() }
} }
spaceRow.addView(spaceButton) spaceRow.addView(spaceButton)
@ -138,10 +157,14 @@ class MyKeyboardService : InputMethodService() {
inputConnection.deleteSurroundingText(1, 0) inputConnection.deleteSurroundingText(1, 0)
if (currentInput.isNotEmpty()) { if (currentInput.isNotEmpty()) {
currentInput.deleteCharAt(currentInput.length - 1) currentInput = currentInput.substring(0, currentInput.length - 1)
} }
Log.d("MyKeyboardService", "Current input after backspace: $currentInput") Log.d("MyKeyboardService", "Current input after backspace: $currentInput")
} }
private fun onSpace(){
val inputConnection = currentInputConnection ?: return
inputConnection.commitText(" ", 1)
}
override fun onDestroy() { override fun onDestroy() {
@ -151,7 +174,7 @@ class MyKeyboardService : InputMethodService() {
private fun onKeyPressed(text: String) { private fun onKeyPressed(text: String) {
Log.d("MyKeyboardService", "onKeyPressed called with text: $text") Log.d("MyKeyboardService", "onKeyPressed called with text: $text")
val inputConnection: InputConnection = currentInputConnection ?: return val inputConnection: InputConnection = currentInputConnection ?: return
currentInput.append(text) currentInput += text
Log.d("MyKeyboardService", "Current input: $currentInput") Log.d("MyKeyboardService", "Current input: $currentInput")
Log.d("MyKeyboardService", "Key pressed: $text") Log.d("MyKeyboardService", "Key pressed: $text")
@ -210,15 +233,10 @@ class MyKeyboardService : InputMethodService() {
} }
private fun onSuggestionClicked(selectedText: String) { private fun onSuggestionClicked(selectedText: String) {
val inputConnection: InputConnection = currentInputConnection ?: return val inputConnection: InputConnection = currentInputConnection ?: return
// Delete the current input
for (i in currentInput.indices) {
inputConnection.deleteSurroundingText(1, 0)
}
// Commit the selected text // Commit the selected text
inputConnection.commitText(selectedText, 1) inputConnection.commitText(selectedText, 1)
// Update the current input // Update the current input
currentInput.clear() currentInput = ""
currentInput.append(selectedText)
// Clear suggestions // Clear suggestions
suggestionsContainer.removeAllViews() suggestionsContainer.removeAllViews()
} }