Contenidos
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/editText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:hint="Introduce texto aquí" /> <Button android:id="@+id/buttonSimulateKeyPress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/editText" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="Simular Tecla 'A'" /> </RelativeLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.example.myapplication import android.os.Bundle import android.view.KeyEvent import android.widget.Button import android.widget.EditText import androidx.activity.ComponentActivity class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val editText = findViewById<EditText>(R.id.editText) val buttonSimulateKeyPress = findViewById<Button>(R.id.buttonSimulateKeyPress) buttonSimulateKeyPress.setOnClickListener { simulateKeyPress(editText) } } private fun simulateKeyPress(editText: EditText) { val keyEventDown = KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A) editText.dispatchKeyEvent(keyEventDown) val keyEventUp = KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A) editText.dispatchKeyEvent(keyEventUp) } } |