如何删除可重复的键,Android自定义键盘的键预览

时间:2015-02-23 04:58:49

标签: java android android-softkeyboard

如何删除重复键,Android自定义键盘的键预览。我使用以下方法删除我想要的键的关键预览。但是该方法不适用于可重复的密钥。 (android:isRepeatable =“true”)如果密钥不可重复,则以下方法正在运行。

删除密钥xml

<Key android:codes="-5"
        android:keyWidth="13%p"
        android:keyIcon="@drawable/ic_key_delete_white"
        android:keyBackground="@color/dark_key_background"
        android:isRepeatable="true"
        android:horizontalGap="3.5%p"
        android:keyEdgeFlags="right"/>

输入法服务类

@Override
public void onPress(int primaryCode) {
    if (primaryCode == -1 
            || primaryCode == -5){
        kv.setPreviewEnabled(false);
    }
}

@Override
public void onRelease(int primaryCode) {
    if(primaryCode == -1 
            || primaryCode == -5){
        kv.setPreviewEnabled(true);
    }
}

4 个答案:

答案 0 :(得分:4)

Revers setPreviewEnabled flag ..

public void onCreate() {

    mInputView.setPreviewEnabled(false);

}

public void onPress(int primaryCode) {
    if (primaryCode==-1||primaryCode==-2||primaryCode==-5||primaryCode==-4){

    } else {
       mInputView.setPreviewEnabled(true);
    }
}

public void onRelease(int primaryCode) {
    mInputView.setPreviewEnabled(false);
}

答案 1 :(得分:0)

在下面的示例中,我们有两个可重复的键(空格和删除)

第1步:将PreviewEnabled设置为&#34; false&#34;在onCreateInputView()中:

kv.setPreviewEnabled(false);

第2步:在onPress()中添加以下if-else语句:

public void onPress(int primaryCode) 
{
   if (!(primaryCode==32||primaryCode==Keyboard.KEYCODE_DELETE))
   {
       kv.setPreviewEnabled(true);
   }
   else
   {
       kv.setPreviewEnabled(false);
   }
}

步骤3:将PreviewEnabled设置为&#34; false&#34; in onRelease():

public void onRelease(int primaryCode) 
{
   kv.setPreviewEnabled(false);
}

答案 2 :(得分:0)

我已经写了一个答案,请在这里查看 https://stackoverflow.com/a/53446268/5689605

要放在这里,

这是修改后的Keyboard类

import android.content.Context
import android.inputmethodservice.KeyboardView
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import com.continental.testapplication.utils.dpToPx
import java.lang.reflect.Method

class ModifiedKeyboardView :KeyboardView{
    constructor(context: Context, attrs: AttributeSet):super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int):super(context, attrs, defStyleAttr)
    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr:Int, defStyleRes:Int):
            super(context, attrs, defStyleAttr, defStyleRes)

    /**
     * Return true, if preview is to be shown, false otherwise. If not implemented,
     * the preview is going to be shown.....
     */
    var keyPreviewIndexListener:((Int)->Boolean) ?= null

    private val findKeyIndicesMethod:Method = KeyboardView::class.java.getDeclaredMethod(
            "getKeyIndices",Int::class.java,Int::class.java, (IntArray::class).java).also {
        it.isAccessible = true
    }
    private val previewText:TextView = KeyboardView::class.java.getDeclaredField(
            "mPreviewText").let {
        it.isAccessible = true
        it.get(this) as TextView
    }


    override fun onTouchEvent(me: MotionEvent?): Boolean {
        if(me == null) return super.onTouchEvent(me)
        when(me.action){
            MotionEvent.ACTION_DOWN -> isPreviewEnabled = true
            MotionEvent.ACTION_MOVE -> {
                val touchX = me.x - paddingLeft
                var touchY = me.y.toInt() - paddingTop
                val verticalCorrection = dpToPx(14f, context)
                if (touchY >= -verticalCorrection)
                    touchY += verticalCorrection.toInt()

                val keyIndex:Int = findKeyIndicesMethod.invoke(this, touchX.toInt(), touchY.toInt(), null) as Int

                    isPreviewEnabled = keyPreviewIndexListener?.invoke(keyIndex)?:true
                    if(!isPreviewEnabled){
                        previewText.visibility = View.INVISIBLE
                    }


            }
        }
        return super.onTouchEvent(me)
    }
}

原样粘贴。

接下来,在您要操作键盘的类中,

 keyboardView.keyPreviewIndexListener = {
            it != spaceIndex && it != doneIndex && it != deleteIndex && it != `your_custom_index`
        }

要找到索引,您可以执行以下操作

 doneIndex = keyboardView.keyboard.keys.indexOfFirst {
            it.codes[0] == Keyboard.KEYCODE_DONE
        }

这将防止移动。请同时附加其他解决方案。 即

override fun onPress(primaryCode: Int) {
            Log.e("onPress", primaryCode.toString())
            checkAndActivatePreview(primaryCode)

        }

        override fun onRelease(primaryCode: Int) {
            Log.e("onRelease", primaryCode.toString())
            deactivatePreview()
        }

 private fun checkAndActivatePreview(primaryCode: Int) {
        keyboard.isPreviewEnabled =
                (primaryCode != `your_custom_code`
                && primaryCode != SPACE_KEY_CODE && primaryCode != Keyboard.KEYCODE_DELETE
                && primaryCode != Keyboard.KEYCODE_DONE)
    }

答案 3 :(得分:0)

如果键是可重复的,OnRelease将在调用OnPress后立即被调用。 使用此代码

@Override
public void onPress(int primaryCode) {
    if (primaryCode == -1 
            || primaryCode == -5){
        kv.setPreviewEnabled(false);
    }else{
        kv.setPreviewEnabled(true);
    }
}

@Override
public void onRelease(int primaryCode) {

}