2012-03-19 16 views
6

मैं वर्तमान में एक कस्टम कीबोर्ड ऐप विकसित करने पर काम कर रहा हूं जिसे डीपीएड का उपयोग करके अपने प्राथमिक इनपुट डिवाइस के रूप में अनुकूलित किया जाएगा।कस्टम एंड्रॉइड कीबोर्ड फोकस इश्यू

मेरी समस्या यह है कि जब कर्सर एडिटटेक्स्ट फ़ील्ड में होता है और आप नीचे दबाते हैं (उदा। KEYCODE_DPAD_DOWN), कीबोर्ड दृश्य फोकस और KeyEvents प्राप्त नहीं कर रहा है। या तो कुछ भी नहीं होता है, या प्रश्न में EditText के नीचे तत्व फोकस प्राप्त करता है।

नीचे प्रासंगिक कोड है।

किसी भी मदद की बहुत सराहना की जाएगी। मैंने सफलता के बिना संकेतों के लिए सॉफ़्टकेबोर्ड उदाहरण के साथ-साथ KeyboardView.java को विच्छेदन करने का प्रयास किया है।

धन्यवाद, ब्रायन

MyKeyboard.java

public class MyKeyboard extends InputMethodService { 

    private static final String TAG = "MyKeyboard"; 
    private MyKeyboardView mInputView = null; 

    @Override public void onCreate() { 
     super.onCreate(); 
    } 

    @Override public View onCreateInputView() { 
     mInputView = (MyKeyboardView) getLayoutInflater().inflate(R.layout.input, null); 

     // attempts to make this focusable 
     mInputView.setClickable(true); 
     mInputView.setFocusableInTouchMode(true); 
     mInputView.setFocusable(true); 
     mInputView.setEnabled(true); 

     return mInputView; 
    } 

    @Override public View onCreateCandidatesView() { 
     super.onCreateCandidatesView(); 
     return null; 
    } 

    @Override public void onStartInputView(EditorInfo info, boolean restarting) { 
     super.onStartCandidatesView(info, restarting); 
    } 

    @Override public void onFinishInput() { 
     super.onFinishInput(); 
    } 

    @Override public void onDestroy() { 
     super.onDestroy(); 
    } 
} 

MyKeyboardView.java

public class MyKeyboardView extends TableLayout implements View.OnClickListener, View.OnFocusChangeListener { 

    private static final String TAG = "MyKeyboardView"; 
    private ArrayList<Character> charList = new ArrayList<Character>(); 

    public MyKeyboardView(Context context) { 
     super(context); 

     populateKeyboard(); 
     this.setOnFocusChangeListener(this); 
     this.setOnClickListener(this); 
    } 

    public MyKeyboardView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     populateKeyboard(); 
     this.setOnFocusChangeListener(this); 
     this.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View arg0) { 
     Log.d(TAG, "onClick"); 
    } 

    private void populateKeyboard() { 
     charList.add(new Character(',')); 
     charList.add(new Character('.')); 
     charList.add(new Character('?')); 
     charList.add(new Character('<')); 
     charList.add(new Character('>')); 
     charList.add(new Character((char) 0x2798)); // arrow 
     charList.add(new Character((char) 0x2798)); // arrow 
     charList.add(new Character((char) 0x2798)); // arrow 
     charList.add(new Character((char) 0x005F)); // underscore 
     for(char c = '@'; c < 'Z'; c++) { 
      charList.add(new Character(c)); 
      Log.d(TAG, "char: " + c); 
     } 


     TableRow tr = null; 
     for(int i=0; i<charList.size(); i++) { 
      if(i % 7 == 0) { 
       if(tr != null) 
        this.addView(tr); 
       tr = new TableRow(this.getContext()); 
       tr.setGravity(Gravity.CENTER_HORIZONTAL); 
      } 
      TextView tv = new TextView(this.getContext()); 
      tv.setPadding(21, 2, 21, 2); 
      tv.setText(charList.get(i).toString()); 
      tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22); 
      tv.setTextColor(Color.WHITE); 
      tv.setGravity(Gravity.CENTER); 
      tv.setFocusable(true); 
      tv.setEnabled(true); 

      tr.addView(tv); 
     } 
     if(tr.getChildCount() > 0) 
      this.addView(tr); 
    } 

    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     Log.d(TAG, "mInputView onFocusChange " + (hasFocus ? "true" : "false")); 
    } 
} 

input.xml

<?xml version="1.0" encoding="utf-8"?> 
<com.weirdtuesday.mykeyboard.MyKeyboardView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/input" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:background="#FF000000" 
    android:focusable="true" /> 

उत्तर

2

प्रमुख घटनाओं होना चाहिए जनसंपर्क ऑनकी विधि में मैन्युअल रूप से ocessed। कर्सर को स्थानांतरित करने के लिए, मैं इसका उपयोग करता हूं:

if (primaryCode == KeyEvent.KEYCODE_DPAD_RIGHT) { 
     int position = connection.getTextBeforeCursor(Integer.MAX_VALUE, 0) 
       .length(); 
     final CharSequence selected = connection.getSelectedText(0); 
     if (selected != null) 
      connection.commitText(
        mComposing.substring(0, 
          mComposing.length() - selected.length()), 1); 
     else 
      connection.commitText(mComposing, 1); 
     connection.setSelection(position + 1, position + 1); 
    } 
+0

मैंने इसे दूसरे दिन बाहर निकाला! धन्यवाद! सिर्फ स्पष्टीकरण के लिए यह InputMethodService के आपके उप-वर्ग में है। –

+1

@ ब्रायन स्टर्न हाय ब्रायन, क्या आप मुझे बता सकते हैं कि कुंजी पर ध्यान केंद्रित करने के लिए InputMethodService में कौन से परिवर्तन आवश्यक हैं? धन्यवाद – CodeFury

+0

@ श्री क्या आपको कोई समाधान मिला? –

संबंधित मुद्दे