2016-09-02 54 views
0

我有一个显示的数字软键盘Android的一个Java代码:数字软键盘的Android NDK使用

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

但我使用数字键盘相同的方法面临着很多麻烦。任何帮助将是伟大的..谢谢!

+0

的[如何使用,以显示Android的软数字键盘只JNI(可能的复制http://stackoverflow.com/questions/39263351/how-to-show-android-soft-number-keypad-only-only-jni) – Michael

+0

不要再次发布相同的问题和其他一些信息。相反,将该信息编辑为原始问题。 – Michael

+0

我的错误。我删除了上一个问题,它有帮助 – Naruto

回答

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();