定义布局
<TextView
android:id="@+id/input_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="18sp"
android:background="@color/green_title_bar"/>
<com.open.androidtvwidget.keyboard.SkbContainer
android:id="@+id/skbContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</com.open.androidtvwidget.keyboard.SkbContainer>
代码逻辑
@Bind(R.id.skbContainer)
SkbContainer skbContainer;
@Bind(R.id.input_tv)
TextView inputTv;
public void setKeyBoard() {
skbContainer.setSkbLayout(R.xml.skb_all_key);
skbContainer.setFocusable(true);
skbContainer.setFocusableInTouchMode(true);
// 设置属性(默认是不移动的选中边框)
setSkbContainerMove();
//
skbContainer.setSelectSofkKeyFront(false); // 设置选中边框最前面.
// 监听键盘事件.
skbContainer.setOnSoftKeyBoardListener(new SoftKeyBoardListener() {
@Override
public void onCommitText(SoftKey softKey) {
int keyCode = softKey.getKeyCode();
String keyLabel = softKey.getKeyLabel();
if (!TextUtils.isEmpty(keyLabel)) { // 输入文字.
inputTv.setText(inputTv.getText() + softKey.getKeyLabel());
} else { // 自定义按键,这些都是你自己在XML中设置的keycode.
keyCode = softKey.getKeyCode();
if (keyCode == KeyEvent.KEYCODE_DEL) {
String text = inputTv.getText().toString();
if (TextUtils.isEmpty(text)) {
Toast.makeText(getApplicationContext(), "文本已空", Toast.LENGTH_LONG).show();
} else {
inputTv.setText(text.substring(0, text.length() - 1));
}
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {
keyboard.setVisibility(View.GONE);
}
}
}
@Override
public void onBack(SoftKey key) {
finish();
}
@Override
public void onDelete(SoftKey key) {
String text = inputTv.getText().toString();
inputTv.setText(text.substring(0, text.length() - 1));
}
});
// DEMO(测试键盘失去焦点和获取焦点)
skbContainer.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
OPENLOG.D("hasFocus:" + hasFocus);
if (hasFocus) {
if (mOldSoftKey != null)
skbContainer.setKeySelecte