使用ndk的android中的数字软键盘

时间:2016-09-02 06:30:33

标签: java-native-interface

我有一个在Android中显示数字软键盘的java代码:

public class MainActivity extends Activity {
    EditText ed1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1 = (EditText) findViewById(R.id.editText1);
        ed1.setInputType(InputType.TYPE_CLASS_NUMBER);
    }
}

我的activity_main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="numberkeypad.inputmethod.MainActivity" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:ems="10" >
</EditText>

输出为:数字软键盘

enter image description here

我想使用NDK JNI调用显示相同的键盘,而不是EditText。我使用以下链接以这种方式实现了默认键盘:

How to show the soft keyboard on native activity

但是我使用相同的数字键盘方法遇到了很多麻烦。任何帮助都会很棒。谢谢!

1 个答案:

答案 0 :(得分:0)

无法找到直接执行此操作的方法,必须覆盖View类的onCreateInputConnection方法,然后使用重写方法对函数进行JNI调用。

public class NumbersView extends View {
    public NumbersView(Context context) {
        super(context);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        InputConnection inputConnection =  super.onCreateInputConnection(outAttrs);
        switch(SystemKeyboardType){
            case InputType.TYPE_CLASS_PHONE:
                outAttrs.inputType |= InputType.TYPE_CLASS_PHONE;
                break;
            case InputType.TYPE_CLASS_TEXT:
                outAttrs.inputType |= InputType.TYPE_CLASS_TEXT;
                break;
            case InputType.TYPE_CLASS_NUMBER:
                outAttrs.inputType |= InputType.TYPE_CLASS_NUMBER;
                break;
            case InputType.TYPE_CLASS_DATETIME:
                outAttrs.inputType |= InputType.TYPE_CLASS_DATETIME;
                break;
            default:
                outAttrs.inputType |= InputType.TYPE_CLASS_TEXT;
                break;
        }

        return inputConnection;
    }
}

 **/
@Override
protected void onCreate(Bundle savedInstanceState) {
    calculateDeviceDPI();
    super.onCreate(savedInstanceState);
    myView = new NumbersView(getApplicationContext());
    addContentView(myView,new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    myView.setFocusable(true);
    myView.setFocusableInTouchMode(true);
    //myView.requestFocus();

    mContext = this;
}

public void displaySystemKeyboard(String keyboardType){
    if(keyboardType.equals("text")) {
        SystemKeyboardType = InputType.TYPE_CLASS_TEXT;
    }
    else if(keyboardType.equals("phone")) {
        SystemKeyboardType = InputType.TYPE_CLASS_PHONE;
    }
    else if(keyboardType.equals("number")) {
        SystemKeyboardType = InputType.TYPE_CLASS_NUMBER;
    }
    else if(keyboardType.equals("datetime")) {
        SystemKeyboardType = InputType.TYPE_CLASS_DATETIME;
    }
    else {
        SystemKeyboardType = InputType.TYPE_CLASS_DATETIME;
    }


    Context ctx = getApplicationContext();

    InputMethodManager mgr = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);

    myView.requestFocus();
    // only will trigger it if no physical keyboard is open
    mgr.restartInput(myView);
    mgr.showSoftInput(myView, 0);
}

public void hideSystemKeyboard(){
    Context ctx = getApplicationContext();
    View myView = this.getWindow().getDecorView();
    InputMethodManager mgr = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(myView.getWindowToken(), 0);
}

最后对函数进行了JNI调用:

 if(pShow){
    jmethodID showSysKeys = lJNIEnv->GetMethodID(lClassDeviceAPI,"displaySystemKeyboard","(Ljava/lang/String;)V");
    if(showSysKeys == NULL){
         LOGI("displaySystemKeyboard::Couldn't get void displaySystemKeyboard Method");
         return;
    }

    jstring keyboardType = lJNIEnv->NewStringUTF(KeyboardType.c_str());
    if(!keyboardType)
    {
        LOGI( "failed to alloc param string in java." );
        return;
    };

    lJNIEnv->CallVoidMethod(lObjDeviceAPI,showSysKeys, keyboardType);
}
else{
    jmethodID hideSysKeys = lJNIEnv->GetMethodID(lClassDeviceAPI,"hideSystemKeyboard","()V");
    if(hideSysKeys == NULL){
         LOGI("hideSystemKeyboard::Couldn't get void hideSystemKeyboard Method");
         return;
    }

    lJNIEnv->CallVoidMethod(lObjDeviceAPI,hideSysKeys);
}

lJavaVM->DetachCurrentThread();