我如何控制自定义键盘键?

时间:2016-10-04 07:11:08

标签: android keyboard

您好我有自定义键盘或Numpad如何取消特定号码?我正在构建一个游戏,您可以选择4个数字(4132)然后按回车键,这样我想要点击的这个数字是不可点击的,因为他只需按一次数字。只有当他退格时我才能再次点击例如,数字2将再次可点击 更多解释当4按下(关闭)然后编号1相同的东西然后其他3-2如果用户后退空间3-2它可以再次点击和thx

enter image description here

public class BasicOnKeyboardActionListener implements OnKeyboardActionListener {

private Activity mTargetActivity;


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);
}

CustomKeyboardView.class

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);
}

KeyboardWidgetTutorialActivity.class

public class KeyboardWidgetTutorialActivity extends Activity {

private CustomKeyboardView mKeyboardView;
private View mTargetView;
private Keyboard mKeyboard;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    mKeyboard = new Keyboard(this, R.xml.keyboard);
    mTargetView = (EditText) findViewById(R.id.target);
    mTargetView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            showKeyboardWithAnimation();
            return true;
        }
    });

    mKeyboardView = (CustomKeyboardView) findViewById(R.id.keyboard_view);
    mKeyboardView.setKeyboard(mKeyboard);
    mKeyboardView
            .setOnKeyboardActionListener(new BasicOnKeyboardActionListener(
                    this));
}


private void showKeyboardWithAnimation() {
    if (mKeyboardView.getVisibility() == View.GONE) {
        Animation animation = AnimationUtils
                .loadAnimation(KeyboardWidgetTutorialActivity.this,
                        R.anim.slide_in_bottom);
        mKeyboardView.showWithAnimation(animation);
    }
    @Override
    public void onBackPressed() {
         if (mKeyboardView.isShown()) {
          mKeyboardView.setVisibility(View.GONE);
        } else {
           super.onBackPressed();
        }
     }

  }
}

这是主要的xml

  <RelativeLayout android:id="@+id/LinearLayout1"
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:layout_width="fill_parent"
    android:id="@+id/container" android:layout_alignParentTop="true"
    android:layout_height="fill_parent"     android:layout_above="@+id/keyboard_view">
    <EditText android:layout_width="fill_parent" android:id="@+id/target"
        android:layout_height="wrap_content" />
</LinearLayout>

<it.anddev.tutorial.CustomKeyboardView
    android:id="@+id/keyboard_view" android:visibility="gone"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">   </it.anddev.tutorial.CustomKeyboardView>

  </RelativeLayout>

keyboard xml

 <Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
android:keyWidth="33%p" android:horizontalGap="0px"
android:verticalGap="0px" android:keyHeight="150dip">

<Row>
    <Key android:codes="8" android:keyLabel="1" android:keyEdgeFlags="left"      />
    <Key android:codes="9" android:keyLabel="2" />
    <Key android:codes="10" android:keyLabel="3" />
    <Key android:codes="67" android:keyIcon="@drawable/sym_keyboard_delete"
        android:iconPreview="@drawable/sym_keyboard_delete"
        android:keyEdgeFlags="right" />
</Row>

<Row>
    <Key android:codes="11" android:keyLabel="4" android:keyEdgeFlags="left" />
    <Key android:codes="12" android:keyLabel="5" />
    <Key android:codes="13" android:keyLabel="6" />
    <Key android:codes="66" android:keyEdgeFlags="right"
        android:keyIcon="@drawable/sym_keyboard_feedback_return"
        android:iconPreview="@drawable/sym_keyboard_feedback_return" />
</Row>

<Row>
    <Key android:codes="14" android:keyLabel="7" android:keyEdgeFlags="left" />
    <Key android:codes="15" android:keyLabel="8" />
    <Key android:codes="16" android:keyLabel="9"    android:keyEdgeFlags="right" />
    <Key android:codes="7" android:keyLabel="0" />

    </Row>


  </Keyboard>

0 个答案:

没有答案