diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/sortug/thaiinput/MyKeyboardService.kt b/app/src/main/java/com/sortug/thaiinput/MyKeyboardService.kt index 6655731..bf6222e 100644 --- a/app/src/main/java/com/sortug/thaiinput/MyKeyboardService.kt +++ b/app/src/main/java/com/sortug/thaiinput/MyKeyboardService.kt @@ -11,16 +11,23 @@ import com.google.gson.Gson import android.widget.Button import android.widget.HorizontalScrollView import android.widget.LinearLayout - +import android.widget.TextView +import kotlin.properties.Delegates class MyKeyboardService : InputMethodService() { 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 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 { @@ -41,10 +48,22 @@ class MyKeyboardService : InputMethodService() { 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(suggestionsContainer, 0) - inputView.addView(scrollView, 0) + inputView.addView(currentInputDisplay) + scrollView.addView(suggestionsContainer) + inputView.addView(scrollView) setupKeyboardKeys(inputView) return inputView } @@ -104,7 +123,7 @@ class MyKeyboardService : InputMethodService() { LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT ) - setOnClickListener { onKeyPressed(" ") } + setOnClickListener { onSpace() } } spaceRow.addView(spaceButton) @@ -138,10 +157,14 @@ class MyKeyboardService : InputMethodService() { inputConnection.deleteSurroundingText(1, 0) if (currentInput.isNotEmpty()) { - currentInput.deleteCharAt(currentInput.length - 1) + currentInput = currentInput.substring(0, currentInput.length - 1) } Log.d("MyKeyboardService", "Current input after backspace: $currentInput") } + private fun onSpace(){ + val inputConnection = currentInputConnection ?: return + inputConnection.commitText(" ", 1) + } override fun onDestroy() { @@ -151,7 +174,7 @@ class MyKeyboardService : InputMethodService() { private fun onKeyPressed(text: String) { Log.d("MyKeyboardService", "onKeyPressed called with text: $text") val inputConnection: InputConnection = currentInputConnection ?: return - currentInput.append(text) + currentInput += text Log.d("MyKeyboardService", "Current input: $currentInput") Log.d("MyKeyboardService", "Key pressed: $text") @@ -210,15 +233,10 @@ class MyKeyboardService : InputMethodService() { } private fun onSuggestionClicked(selectedText: String) { val inputConnection: InputConnection = currentInputConnection ?: return - // Delete the current input - for (i in currentInput.indices) { - inputConnection.deleteSurroundingText(1, 0) - } // Commit the selected text inputConnection.commitText(selectedText, 1) // Update the current input - currentInput.clear() - currentInput.append(selectedText) + currentInput = "" // Clear suggestions suggestionsContainer.removeAllViews() }