2012-03-19 4 views
6

현재 기본 입력 장치로 DPad를 사용하는 기기에 최적화 된 맞춤형 키보드 앱을 개발 중입니다.맞춤 Android 키보드 포커스 문제

내 문제는 커서가 EditText 필드에 있고 키를 누를 때 (예 : KEYCODE_DPAD_DOWN) 키보드보기가 포커스와 KeyEvent를받지 못한다는 것입니다. 아무 일도 일어나지 않거나 문제의 EditText 아래에있는 요소가 포커스를받습니다.

다음은 관련 코드입니다.

도움을 주시면 감사하겠습니다. 나는 성공없이 힌트를 위해 KeyboardView.java뿐만 아니라 SoftKeyboard 예제를 해부하려했다.

감사합니다, 브라이언

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

중요한 사건이어야 홍보 onKey 메서드에서 수동으로 처리되었습니다. 커서를 이동하려면 다음을 사용하십시오.

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

@BryanStern 안녕하세요. Bryan, InputMethodService에서 키에 포커스를 전달하기 위해 필요한 변경 사항을 알려주세요. 감사합니다 – CodeFury

+0

@Sree 어떤 해결책을 찾았습니까? –