自定义键盘重叠EditText

时间:2014-03-12 14:44:03

标签: android keyboard android-manifest

目前我正在根据我的客户要求开发自定义键盘。除了一个问题,每件事都适合我。我会逐步解释我的问题。

  1. 我的屏幕由底部的2个EditText组成。
  2. 当我点击Edittext时,KeyBoard工作正常,我就是 能够输入值。
  3. 但我的问题是keyBoard是重叠需要的Edittext 所示。
  4. 使用Edittext进行屏幕截图:

    Screen Shot With Edittext

    键盘重叠Edittext:

    Keyboard overlapped Edittext

    如果在Manifest

    中的某些情况下已经解决过了
    <activity android:windowSoftInputMode="adjustPan|adjustResize"> </activity> 
    

    Xml代码

    <?xml version="1.0" encoding="utf-8"?>
    <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
        android:horizontalGap="0dp"
        android:keyHeight="50dp"
        android:keyTextSize="12sp"
        android:keyWidth="25%p"
        android:labelTextSize="12sp"
        android:verticalGap="0dp" >
    
        <Row>
    
            <Key
                android:codes="8"
                android:keyEdgeFlags="left"
                android:keyLabel="1" />
    
            <Key
                android:codes="9"
                android:keyLabel="2" />
    
            <Key
                android:codes="10"
                android:keyLabel="3" />
    
            <Key
                android:codes="69"
                android:keyEdgeFlags="right"
                android:keyLabel="-" />
        </Row>
    
        <Row>
    
            <Key
                android:codes="11"
                android:keyEdgeFlags="left"
                android:keyLabel="4" />
    
            <Key
                android:codes="12"
                android:keyLabel="5" />
    
            <Key
                android:codes="13"
                android:keyLabel="6" />
    
            <Key
                android:codes="56"
                android:keyEdgeFlags="right"
                android:keyLabel="." />
        </Row>
    
        <Row>
    
            <Key
                android:codes="14"
                android:keyEdgeFlags="left"
                android:keyLabel="7" />
    
            <Key
                android:codes="15"
                android:keyLabel="8" />
    
            <Key
                android:codes="16"
                android:keyLabel="9" />
    
            <Key
                android:codes="67"
                android:iconPreview="@drawable/sym_keyboard_delete"
                android:keyEdgeFlags="right"
                android:keyIcon="@drawable/sym_keyboard_delete" />
        </Row>
    
        <Row>
    
            <Key
                android:codes="7"
                android:keyLabel="0"
                android:keyWidth="50%p" />
    
            <Key
                android:codes="66"
                android:keyEdgeFlags="right"
                android:keyLabel="Done"
                android:keyWidth="50%p" />
        </Row>
    
    </Keyboard>
    

    我的Java代码是

    public class CustomKeyboardView extends KeyboardView {
    
        public CustomKeyboardView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public void showWithAnimation(Animation animation) {
            animation.setAnimationListener(new AnimationListener() {
    
                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    setVisibility(View.VISIBLE);
                }
            });
    
            setAnimation(animation);
        }
    
    public class BasicOnKeyboardActionListener implements OnKeyboardActionListener {
    
        private Activity mTargetActivity;
        /***
         * 
         * @param targetActivity
         *            Activity a cui deve essere girato l'evento
         *            "pressione di un tasto sulla tastiera"
         * @param mChangedListener 
         */
        public BasicOnKeyboardActionListener(Activity targetActivity) {
            mTargetActivity = targetActivity;
        }
    
        @Override
        public void swipeUp() {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void swipeRight() {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void swipeLeft() {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void swipeDown() {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onText(CharSequence text) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onRelease(int primaryCode) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onPress(int primaryCode) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            long eventTime = System.currentTimeMillis();
            KeyEvent event = new KeyEvent(eventTime, eventTime,
                    KeyEvent.ACTION_DOWN, primaryCode, 0, 0, 0, 0,
                    KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE);
    
            mTargetActivity.dispatchKeyEvent(event);
    
        }
    

    在活动中声明时

    private Keyboard mKeyboard;
        public CustomKeyboardView mKeyboardView;
    
        private void setCustomKeyBoard(){
    
            mKeyboard = new Keyboard(mContext, R.layout.keyboard);
    
            mKeyboardView = (CustomKeyboardView) findViewById(R.id.keyboard_view);
            mKeyboardView.setPreviewEnabled(false);
            mKeyboardView.setKeyboard(mKeyboard);
    
    
            mKeyboardView.setOnKeyboardActionListener(new BasicOnKeyboardActionListener(this));
    
        }
    

    隐藏和显示KeyBoard

    /** Make the CustomKeyboard visible, and hide the system keyboard for view v. */
        public void showCustomKeyboard( View v ) {
    
            mKeyboardView.setVisibility(View.VISIBLE);
            mKeyboardView.setEnabled(true);
    
            if( v!=null ){
                ((InputMethodManager)mContext.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
    
            } 
        }
    
        /** Make the CustomKeyboard invisible. */
        public void hideCustomKeyboard() {
            mKeyboardView.setVisibility(View.GONE);
            mKeyboardView.setEnabled(false);
    
        }
    

    当我点击EditText时,下面的方法会触发

    registerEditText(R.id.edtStop);
    
    private void registerEditText(int resid) {
    
            // Find the EditText 'resid'
            EditText edittext= (EditText)mTradeCommonFieldsView.findViewById(resid);
    
            // Make the custom keyboard appear
            edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
    
    
                @Override 
                public void onFocusChange(View v, boolean hasFocus) {
    
                    if(hasFocus){
    
                        mTradeActivity.showCustomKeyboard(v);
    
                    }else{
                        mTradeActivity.hideCustomKeyboard();
                    }
                }
            });
            edittext.setOnClickListener(new OnClickListener() {
                @Override 
                public void onClick(View v) {
    
                    mTradeActivity.showCustomKeyboard(v);
                }
            });
            // Disable standard keyboard hard way
            edittext.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
                    return true; // Consume touch event
                }
            });
            // Disable spell check (hex strings look like words to Android)
            edittext.setInputType( edittext.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS );
        }
    

    FOLL

1 个答案:

答案 0 :(得分:3)

我的自定义IME遇到了类似的问题,并将以下代码添加到我的InputMethodService子类中为我修复了它:

@Override public void onComputeInsets(InputMethodService.Insets outInsets) { super.onComputeInsets(outInsets); if (!isFullscreenMode()) { outInsets.contentTopInsets = outInsets.visibleTopInsets; } }