3
votes

In native code (java) I can create TextInput with handle onKeyPreIme

Like this

com/myapp/CustomEditText.java
package com.myapp;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.widget.EditText;

public class CustomEditText extends EditText {

  public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        return true;
    }
    return false;
  }
}

xml

// res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<com.myapp.CustomEditText
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/someid"
  android:layout_height="wrap_content"
  android:layout_width="match_parent"
/>

MainActivity.java

// com/myapp/MainActivity.java

package com.myapp;

import android.os.Bundle;
import com.facebook.react.ReactActivity;

public class MainActivity extends ReactActivity {
...
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); //this line test TextInput
    ...  
  }
}

Does react native support to do this in TextInput ?

Thank for any help.

Edit : My goal is stop back press while keyboard shown. In native is easy with onKeyPreIme. I try with @Override onBackPress and BackHandler of React native but does not work, too.

// MainActivity.java

@Override
public void onBackPressed() {
  return;
}

This code not work while keyboard shown.

1
You can easily do it with BackHandler and Keyboard component of react-native. Just check is keyboard or shown or not then handle it in BackHandler. - Aditya
@Heisen-Berg How to use BackHandler for force keyboard do not hide while keyboard shown in android ? I already try but keyboard still hide. I test by add console.log to BackHandler but console not show anything on press Back while keyboard shown. - Tron Natthakorn

1 Answers

2
votes
const blackBack = () => true;

Keyboard.addListener('keyboardDidShow', () => {
    BackHandler.addListener('hardwareBackPress', blackBack)
});

Keyboard.addListener('keyboardDidHide', () => {
    BackHandler.removeListener('hardwareBackPress', blackBack)
});