最近做公司的项目,用到一款自定义的软键盘,觉得挺好看的,在这里分享出来,给网友们借鉴下,希望对大家有用~
效果图如下:
核心代码如下:
private void showKeyboard(EditText etContent) {
this.inputContent = etContent;
View content = View.inflate(this, R.layout.keyborad, null);
ll_key_number_page_1 = (LinearLayout) content.findViewById(R.id.ll_keyboard_number_page_1);
ll_key_number_page_2 = (LinearLayout) content.findViewById(R.id.ll_keyboard_number_page_2);
ll_key_char_lower = (LinearLayout) content.findViewById(R.id.ll_keyboard_lower_char);
ll_key_char_upper = (LinearLayout) content.findViewById(R.id.ll_keyboard_upper_char);
for (int i = 1; i <= 122; i++) {
int resId = getResourdIdByResourdName("tv_" + i);
if (resId > 0) {
View view = content.findViewById(resId);
if (view != null) {
view.setOnClickListener(this);
}
}
}
for (int i = 1; i <= 4; i++) {
int ivResId = getResourdIdByResourdName("iv_" + i);
final ImageView iv_empty = (ImageView) content.findViewById(ivResId);
iv_empty.setOnClickListener(this);
iv_empty.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
iv_empty.setBackground(getResources().getDrawable(R.drawable.key_focus));
iv_empty.setImageDrawable(getResources().getDrawable(R.drawable.empty_focus));
} else {
iv_empty.setBackground(getResources().getDrawable(R.drawable.key));
iv_empty.setImageDrawable(getResources().getDrawable(R.drawable.empty_unfocus));
}
}
});
}
for (int i = 1; i <= 2; i++) {
int ivResId = getResourdIdByResourdName("iv_character_" + i);
final ImageView iv_char = (ImageView) content.findViewById(ivResId);
iv_char.setOnClickListener(this);
iv_char.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
iv_char.setBackground(getResources().getDrawable(R.drawable.key_focus));
} else {
iv_char.setBackground(getResources().getDrawable(R.drawable.key));
}
}
});
}
windows = new PopupWindow(content, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
// !!!重要监听返回键
windows.setBackgroundDrawable(new BitmapDrawable());
windows.showAsDropDown(etContent, 0, 0);
key_char = getString(R.string.key_char_value);
key_character = getString(R.string.key_character_value);
key_page = getString(R.string.key_page_value);
key_lower = getString(R.string.key_character_lower_value);
key_upper = getString(R.string.key_character_upper_value);
key_empty = getString(R.string.key_empty_value);
key_del = getString(R.string.key_del_value);
key_finish = getString(R.string.key_finish_value);
}
具体的源码已经上传到CSDN,可以自行下载,地址如下:
http://download.csdn.net/download/downloadname/10122408