Allow showing different soft keyboard in android

This commit is contained in:
Benau 2019-05-27 16:34:16 +08:00
parent 420c51955e
commit 336300b997
10 changed files with 75 additions and 18 deletions

View File

@ -40,6 +40,7 @@ public class STKEditText extends EditText
public STKEditText(Context context)
{
super(context);
setInputType(InputType.TYPE_CLASS_TEXT);
setFocusableInTouchMode(true);
m_composing_start = 0;
m_composing_end = 0;
@ -72,7 +73,7 @@ public class STKEditText extends EditText
super.onCreateInputConnection(out_attrs), this);
}
out_attrs.actionLabel = null;
out_attrs.inputType = InputType.TYPE_CLASS_TEXT;
out_attrs.inputType = getInputType();
out_attrs.imeOptions = EditorInfo.IME_ACTION_NEXT |
EditorInfo.IME_FLAG_NO_FULLSCREEN |
EditorInfo.IME_FLAG_NO_EXTRACT_UI;
@ -178,4 +179,26 @@ public class STKEditText extends EditText
// ------------------------------------------------------------------------
public STKInputConnection getSTKInputConnection()
{ return m_stk_input_connection; }
// ------------------------------------------------------------------------
public void configType(final int type)
{
// Check text_box_widget.hpp for definition
switch (type)
{
case 0:
setInputType(InputType.TYPE_CLASS_TEXT);
break;
case 1:
setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;
case 2:
setInputType(InputType.TYPE_CLASS_NUMBER);
break;
case 3:
setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
break;
default:
break;
}
}
}

View File

@ -140,7 +140,7 @@ public class SuperTuxKartActivity extends NativeActivity
return ret;
}
// ------------------------------------------------------------------------
public void showKeyboard()
public void showKeyboard(final int type)
{
final Context context = this;
// Need to run in ui thread as it access the view m_stk_edittext
@ -157,6 +157,7 @@ public class SuperTuxKartActivity extends NativeActivity
if (m_stk_edittext == null)
createSTKEditText();
m_stk_edittext.configType(type);
m_stk_edittext.setVisibility(View.VISIBLE);
m_stk_edittext.requestFocus();
@ -182,7 +183,7 @@ public class SuperTuxKartActivity extends NativeActivity
/* Called by STK in JNI. */
public void fromSTKEditBox(final int widget_id, final String text,
final int selection_start,
final int selection_end)
final int selection_end, final int type)
{
runOnUiThread(new Runnable()
{
@ -191,6 +192,7 @@ public class SuperTuxKartActivity extends NativeActivity
{
if (m_stk_edittext == null)
createSTKEditText();
m_stk_edittext.configType(type);
m_stk_edittext.setTextFromSTK(widget_id, text, selection_start,
selection_end);
}

View File

@ -299,7 +299,7 @@ namespace irr
virtual u32 getScreenHeight() const = 0;
virtual u32 getOnScreenKeyboardHeight() const = 0;
virtual s32 getMovedHeight() const = 0;
virtual void toggleOnScreenKeyboard(bool show) = 0;
virtual void toggleOnScreenKeyboard(bool show, s32 type = 0) = 0;
virtual void registerGetMovedHeightFunction(HeightFunc) = 0;
//! Check if a driver type is supported by the engine.

View File

@ -1196,7 +1196,7 @@ wchar_t CIrrDeviceAndroid::getKeyChar(SEvent& event)
return key_char;
}
void CIrrDeviceAndroid::toggleOnScreenKeyboard(bool show)
void CIrrDeviceAndroid::toggleOnScreenKeyboard(bool show, s32 type)
{
if (!Android)
return;
@ -1234,7 +1234,12 @@ void CIrrDeviceAndroid::toggleOnScreenKeyboard(bool show)
return;
}
jmethodID method_id = env->GetMethodID(class_native_activity, show ? "showKeyboard" : "hideKeyboard", "()V");
jmethodID method_id = NULL;
if (show)
method_id = env->GetMethodID(class_native_activity, "showKeyboard", "(I)V");
else
method_id = env->GetMethodID(class_native_activity, "hideKeyboard", "()V");
if (method_id == NULL)
{
os::Printer::log("showKeyboard unable to find method id.", ELL_ERROR);
@ -1245,14 +1250,17 @@ void CIrrDeviceAndroid::toggleOnScreenKeyboard(bool show)
return;
}
env->CallVoidMethod(native_activity, method_id);
if (show)
env->CallVoidMethod(native_activity, method_id, (jint)type);
else
env->CallVoidMethod(native_activity, method_id);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();
}
}
void CIrrDeviceAndroid::fromSTKEditBox(int widget_id, const core::stringw& text, int selection_start, int selection_end)
void CIrrDeviceAndroid::fromSTKEditBox(int widget_id, const core::stringw& text, int selection_start, int selection_end, int type)
{
if (!Android)
return;
@ -1290,7 +1298,7 @@ void CIrrDeviceAndroid::fromSTKEditBox(int widget_id, const core::stringw& text,
return;
}
jmethodID method_id = env->GetMethodID(class_native_activity, "fromSTKEditBox", "(ILjava/lang/String;II)V");
jmethodID method_id = env->GetMethodID(class_native_activity, "fromSTKEditBox", "(ILjava/lang/String;III)V");
if (method_id == NULL)
{
os::Printer::log("fromSTKEditBox unable to find method id.", ELL_ERROR);
@ -1309,7 +1317,7 @@ void CIrrDeviceAndroid::fromSTKEditBox(int widget_id, const core::stringw& text,
utf8.push_back(0);
jstring jstring_text = env->NewStringUTF(utf8.data());
env->CallVoidMethod(native_activity, method_id, (jint)widget_id, jstring_text, (jint)selection_start, (jint)selection_end);
env->CallVoidMethod(native_activity, method_id, (jint)widget_id, jstring_text, (jint)selection_start, (jint)selection_end, (jint)type);
if (was_detached)
{
Android->activity->vm->DetachCurrentThread();

View File

@ -73,8 +73,8 @@ namespace irr
virtual bool deactivateGyroscope();
virtual bool isGyroscopeActive();
virtual bool isGyroscopeAvailable();
void fromSTKEditBox(int widget_id, const core::stringw& text, int selection_start, int selection_end);
virtual void toggleOnScreenKeyboard(bool show);
void fromSTKEditBox(int widget_id, const core::stringw& text, int selection_start, int selection_end, int type);
virtual void toggleOnScreenKeyboard(bool show, s32 type = 0);
virtual bool supportsTouchDevice() const { return HasTouchDevice; }
virtual bool hasHardwareKeyboard() const;
// ATM if there is touch device we assume native screen keyboard is

View File

@ -123,7 +123,7 @@ namespace irr
virtual u32 getScreenHeight() const { return 0; }
virtual u32 getOnScreenKeyboardHeight() const { return 0; }
virtual s32 getMovedHeight() const { return 0; }
virtual void toggleOnScreenKeyboard(bool show) {}
virtual void toggleOnScreenKeyboard(bool show, s32 type = 0) {}
virtual void registerGetMovedHeightFunction(HeightFunc) {}
//! Returns true if system has touch device

View File

@ -74,6 +74,7 @@ CGUIEditBox::CGUIEditBox(const wchar_t* text, bool border,
m_from_android_edittext = false;
m_composing_start = 0;
m_composing_end = 0;
m_type = (GUIEngine::TextBoxType)0;
#ifdef _DEBUG
setDebugName("CGUIEditBox");
@ -299,7 +300,7 @@ bool CGUIEditBox::OnEvent(const SEvent& event)
{
CIrrDeviceAndroid* dl = dynamic_cast<CIrrDeviceAndroid*>(
irr_driver->getDevice());
dl->fromSTKEditBox(getID(), Text, MarkBegin, MarkEnd);
dl->fromSTKEditBox(getID(), Text, MarkBegin, MarkEnd, m_type);
}
#endif
}
@ -1185,7 +1186,7 @@ void CGUIEditBox::setText(const wchar_t* text)
{
CIrrDeviceAndroid* dl = dynamic_cast<CIrrDeviceAndroid*>(
irr_driver->getDevice());
dl->fromSTKEditBox(getID(), Text, MarkBegin, MarkEnd);
dl->fromSTKEditBox(getID(), Text, MarkBegin, MarkEnd, m_type);
}
#endif
}
@ -1322,7 +1323,7 @@ bool CGUIEditBox::processMouse(const SEvent& event)
if (GUIEngine::ScreenKeyboard::shouldUseScreenKeyboard())
{
if (irr_driver->getDevice()->hasOnScreenKeyboard())
irr_driver->getDevice()->toggleOnScreenKeyboard(true);
irr_driver->getDevice()->toggleOnScreenKeyboard(true, m_type);
else
openScreenKeyboard();
}
@ -1726,7 +1727,7 @@ void CGUIEditBox::setTextMarkers(s32 begin, s32 end)
{
CIrrDeviceAndroid* dl = dynamic_cast<CIrrDeviceAndroid*>(
irr_driver->getDevice());
dl->fromSTKEditBox(getID(), Text, MarkBegin, MarkEnd);
dl->fromSTKEditBox(getID(), Text, MarkBegin, MarkEnd, m_type);
}
#endif
}

View File

@ -15,6 +15,10 @@
using namespace irr;
using namespace gui;
namespace GUIEngine
{
enum TextBoxType: int;
}
class CGUIEditBox : public IGUIEditBox
{
@ -124,6 +128,7 @@ using namespace gui;
void openScreenKeyboard();
s32 getCursorPosInBox() const { return CursorPos; }
s32 getTextCount() const { return (s32)Text.size(); }
void setTextBoxType(GUIEngine::TextBoxType t) { m_type = t; }
protected:
//! Breaks the single text line.
void breakText();
@ -157,6 +162,8 @@ using namespace gui;
s32 MarkBegin;
s32 MarkEnd;
GUIEngine::TextBoxType m_type;
video::SColor OverrideColor;
gui::IGUIFont *OverrideFont, *LastBreakFont;
IOSOperator* Operator;

View File

@ -158,6 +158,13 @@ void TextBoxWidget::setPasswordBox(bool passwordBox, wchar_t passwordChar)
IGUIEditBox* textCtrl = Widget::getIrrlichtElement<IGUIEditBox>();
assert(textCtrl != NULL);
textCtrl->setPasswordBox(passwordBox, passwordChar);
setTextBoxType(TBT_PASSWORD);
}
// -----------------------------------------------------------------------------
void TextBoxWidget::setTextBoxType(TextBoxType t)
{
((MyCGUIEditBox*)m_element)->setTextBoxType(t);
}
// -----------------------------------------------------------------------------

View File

@ -28,6 +28,15 @@
namespace GUIEngine
{
// This enum can allow showing different soft keyboard in android
enum TextBoxType: int
{
TBT_TEXT = 0,
TBT_PASSWORD = 1,
TBT_NUMBER = 2,
TBT_EMAIL = 3,
};
class ITextBoxWidgetListener
{
public:
@ -70,7 +79,7 @@ namespace GUIEngine
irr::core::stringw getText() const;
void setPasswordBox(bool passwordBox, wchar_t passwordChar = L'*');
void setTextBoxType(TextBoxType t);
virtual void elementRemoved();
/** Override method from base class Widget */