2015-10-13 81 views
3

我已經在片段中實現了我自己的自定義鍵盤。當我點擊editText時,鍵盤會打開。但是所有的數字鍵都不起作用,有些鍵如7,0和8可以點擊,但大多數情況下不能。像回車鍵,右和左都正常工作。在Activity中相同的代碼工作得非常好,但在Fragment中實現時卻不起作用。並且onFocus Listerner在這個片段中沒有被調用。什麼是理由?我實現的代碼如下:自定義鍵盤不能在片段中工作

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
View rootView = inflater.inflate(R.layout.fragment_barcode_detail, 
      container, false); 
editText_barcode = (EditText) rootView 
      .findViewById(R.id.editText_barcode); 
// Lookup the KeyboardView 
mKeyboardViewNum = (KeyboardView)  
rootView.findViewById(R.id.numerickeyboardview); 

    mKeyboardView = (KeyboardView) rootView.findViewById(R.id.keyboardview); 
// Numeric Keyboard 
      key = new NumericKeyboard(); 
      mKeyboardNum = new Keyboard(mActivity, 
        R.xml.numeric_keyboard); 
      // Lookup the KeyboardView 
      // Attach the keyboard to the view 
      mKeyboardViewNum.setKeyboard(mKeyboardNum); 
      listKeysNum = mKeyboardNum.getKeys(); 
      // Do not show the preview balloons 
      mKeyboardView.setPreviewEnabled(true); 
      // Install the key handler 
      mKeyboardViewNum 

.setOnKeyboardActionListener(key.mOnKeyboardActionListenerNum); 
      // Numeric Keyboard 
editText_barcode.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      editos = (EditText) v; 
      System.out.println("====onc== "+editos); 
      hideCustomKeyboard(); 
      key.showCustomNumKeyboard(v); 
     } 
    }); 
    editText_barcode.setOnTouchListener(new OnTouchListener() { 

     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      EditText edittext = (EditText) v; 
      int inType = edittext.getInputType(); // Backup the input type 
      edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard 
      edittext.onTouchEvent(event); // Call native handler 
      edittext.setInputType(inType); // Restore input type 
      edittext.setTextIsSelectable(true); 
      return false; 
     } 
    }); 
    editText_barcode.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (hasFocus) { 
       editos = (EditText) v; 
       System.out.println("====onf== "+editos); 
       key.showCustomNumKeyboard(v); 
      } else { 
       key.hideCustomNumKeyboard(); 
      } 

     } 
    }); 
return rootView; 
} 

     private OnKeyboardActionListener mOnKeyboardActionListenerNum = new 
OnKeyboardActionListener() { 
     @Override 
     public void onKey(int primaryCode, int[] keyCodes) { 
      // Here check the primaryCode to see which key is pressed 
      // based on the android:codes property 

      int start = editos.getSelectionStart(); 
      Editable editable = editos.getText(); 

      switch (primaryCode) { 
      case 0: 
       editable.insert(start, "0"); 
       System.out.println("=====0== "+editable); 
       break; 
      case 1: 
       editable.insert(start, "1"); 
       break; 
      case 2: 
       editable.insert(start, "2"); 
       break; 
      case 3: 
       editable.insert(start, "3"); 
       break; 
      case 4: 
       editable.insert(start, "4"); 
       break; 
      case 5: 
       editable.insert(start, "5"); 
       break; 
      case 6: 
       editable.insert(start, "6"); 
       break; 
      case 7: 
       editable.insert(start, "7"); 
       break; 
      case 8: 
       editable.insert(start, "8"); 
       break; 
      case 9: 
       editable.insert(start, "9"); 
       break; 
      case 10: 
       if (!editos.getText().toString().contains(".")) { 
        editable.insert(start, "."); 
       } 
       break; 
      case -1: 
       if (editable != null && start > 0) { 
        editable.delete(start - 1, start); 
       } 
       break; 
      case 100: 
       if (editos == editText_barcode) { 
         hideCustomNumKeyboard(); 
       } 
       break; 
      case 101: 
       if (start > 0) 
        editos.setSelection(start - 1); 
       break; 
      case 201: 
       if (start < editos.length()) 
        editos.setSelection(start + 1); 
       break; 
      } 
     } 

     @Override 
     public void onPress(int arg0) { 
     } 

     @Override 
     public void onRelease(int primaryCode) { 
     } 

     @Override 
     public void onText(CharSequence text) { 
     } 

     @Override 
     public void swipeDown() { 
     } 

     @Override 
     public void swipeLeft() { 
     } 

     @Override 
     public void swipeRight() { 
     } 

     @Override 
     public void swipeUp() { 
     } 
    }; 

和XML是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@color/light_grey" > 

<RelativeLayout 
    android:id="@+id/top" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/listrow_layerlist_background_dark_purple" > 


<android.inputmethodservice.KeyboardView 
    android:id="@+id/keyboardview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:keyPreviewLayout="@layout/kbpreview" 
    android:keyPreviewOffset="12dp" 
    android:visibility="gone" /> 

<android.inputmethodservice.KeyboardView 
    android:id="@+id/numerickeyboardview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:keyPreviewLayout="@layout/kbpreview" 
    android:keyPreviewOffset="12dp" 
    android:visibility="gone" /> 

<ListView 
    android:id="@+id/listView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@id/bottom" 
    android:layout_below="@id/linear2" 
    android:divider="@null" > 
</ListView> 

回答

0

張貼作爲一個答案,因爲我沒有信譽評論...

查看以下文檔鏈接。 https://developer.android.com/reference/android/R.attr.html#keycode

您用作開關輸入的primaryCode參數應該使用這些代碼。 (KeyEvent.KEYCODE_ [0-9]對應7-16)

您可以發佈您的XML文件嗎? 我的假設是,你的android:代碼中的每個鍵都對應於這些KEYCODE_ [0-9]。

如果這是一個正確的假設,那麼您的開關情況7將通過按鍵0,情況8按鍵1等觸發。