android keyboard颜色,Android基于KeyboardView和Keyboard实现自定义软键盘 自定义键盘背景色...
Android基于KeyboardView和Keyboard實現自定義軟鍵盤
在一些特別的情況下我們需要去自定義鍵盤
例如: 銀行app的密碼輸入之類的
笨方法就是直接使用布局寫我們的自定義軟鍵盤
但這樣寫的話我們的代碼量就很多
Android官方提供了KeyboardView和Keyboard兩個類
我們可以用這兩個類去實現自己的軟鍵盤
Demo 已上傳 GitHub
https://github.com/pengchengfuGit/DIYKeyboard.git
1.主界面的布局與代碼
這是我們主界面的xml
android:layout_width="match_parent"
android:background="@android:color/darker_gray"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="50dp" />
android:background="@android:color/white"
android:layout_width="match_parent"
android:keyBackground="@android:color/holo_purple"
android:keyTextColor="#333333"
android:focusable="true"
android:focusableInTouchMode="true"
android:paddingTop="1dp"
android:shadowColor="#FFFFFF"
android:shadowRadius="0.0"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
RelativeLayout>
主界面的Activity 這里寫了個KeyBoardUtil來初始的我們自定義軟鍵盤
public class MainActivity extends Activity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
final KeyboardView keyboard = (KeyboardView) findViewById(R.id.kv_keyboard);
final EditText editText = (EditText) findViewById(R.id.et);
//在我們點EditText的時候彈出我們的軟鍵盤
editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(editText.hasFocus()){
//用來初始化我們的軟鍵盤
new KeyBoardUtil(keyboard,editText).showKeyboard();
}
return false;
}
});
}
}
2.KeyBoard的鍵盤按鈕配置
這是配置我們自定義軟鍵盤的按鈕的
軟鍵盤顯示的按鈕通過xml來配置
這個xml的文件放個res/xml/… 目錄
沒有xml文件夾的 手動創建一個
** keyWidth , keyHeight 可以采用百分比的寫法
** android:keyWidth=”25%p”
android:keyHeight="50dp"
android:keyWidth="25%p"
android:horizontalGap="1px"
android:verticalGap="1px">
android:codes="49"
android:keyLabel="1" />
android:keyLabel="2" />
android:keyLabel="3" />
android:keyLabel="刪除" />
Row>
android:keyLabel="4" />
android:keyLabel="5" />
android:keyLabel="6" />
android:keyHeight="150dp"
android:keyLabel="完成"/>
Row>
android:keyLabel="7" />
android:keyLabel="8" />
android:keyLabel="9" />
Row>
android:keyLabel="." />
android:codes="48"
android:keyLabel="0" />
Row>
Keyboard>
這里解釋下xml里的幾個參數
屬性
說明
keyLabel
按鍵顯示的內容
keyIcon
按鍵顯示的圖標內容
keyWidth
按鍵的寬度
keyHeight
按鍵的高度
horizontalGap
代表按鍵前的間隙水平方向上的
isSticky
按鍵是否是sticky的,就像shift 鍵 具有兩種狀態
isModifier
按鍵是不是功能鍵
keyOutputText
指定按鍵輸出的內容是字符串
isRepeatable
按鍵是可重復的,如果長按鍵可以觸發重復按鍵事件則為true,else為false
keyEdgeFlags
指定按鍵的對齊指令,取值為left或者right
一些特殊的按鍵的codes建議采用系統已經定義好的值
常見的像刪除 確認 取消
public static final int EDGE_LEFT = 1;
public static final int EDGE_RIGHT = 2;
public static final int EDGE_TOP = 4;
public static final int EDGE_BOTTOM = 8;
public static final int KEYCODE_SHIFT = -1;
public static final int KEYCODE_MODE_CHANGE = -2;
public static final int KEYCODE_CANCEL = -3;
public static final int KEYCODE_DONE = -4;
public static final int KEYCODE_DELETE = -5
public static final int KEYCODE_ALT = -6;
3.配置軟鍵盤
配置軟鍵盤中的實現 我把它放在了KeyBoardUtil
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
public class KeyBoardUtil {
private KeyboardView keyboardView;
private EditText editText;
private Keyboard k1;// 自定義鍵盤
public KeyBoardUtil(KeyboardView keyboardView, EditText editText) {
//setInputType為InputType.TYPE_NULL 不然會彈出系統鍵盤
editText.setInputType(InputType.TYPE_NULL);
k1 = new Keyboard(editText.getContext(), R.xml.key);
this.keyboardView = keyboardView;
this.editText = editText;
this.keyboardView.setOnKeyboardActionListener(listener);
this.keyboardView.setKeyboard(k1);
this.keyboardView.setEnabled(true);
this.keyboardView.setPreviewEnabled(false);
}
private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
@Override
public void swipeUp() {
}
@Override
public void swipeRight() {
}
@Override
public void swipeLeft() {
}
@Override
public void swipeDown() {
}
@Override
public void onText(CharSequence text) {
}
@Override
public void onRelease(int primaryCode) {
}
@Override
public void onPress(int primaryCode) {
}
@Override
public void onKey(int primaryCode, int[] keyCodes) {
Editable editable = editText.getText();
int start = editText.getSelectionStart();
switch (primaryCode) {
case Keyboard.KEYCODE_DELETE:
if (editable != null && editable.length() > 0) {
if (start > 0) {
editable.delete(start - 1, start);
}
}
break;
case Keyboard.KEYCODE_CANCEL:
keyboardView.setVisibility(View.GONE);
break;
default:
editable.insert(start, Character.toString((char) primaryCode));
break;
}
}
};
// Activity中獲取焦點時調用,顯示出鍵盤
public void showKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.GONE || visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.VISIBLE);
}
}
// 隱藏鍵盤
public void hideKeyboard() {
int visibility = keyboardView.getVisibility();
if (visibility == View.VISIBLE|| visibility == View.INVISIBLE) {
keyboardView.setVisibility(View.GONE);
}
}
}
這樣我們的軟鍵盤就實現了
自定義鍵盤背景色
有時候我們給不同的按鍵, 設置不同的背景色
像下圖的確定按鈕
這時用原生的KeyboardView和Keyboard是實現不了的
原生的只能所有的按鈕都是統一的樣式
字體顏色 , 按鍵背景色
那如何實現我們不同按鍵不同的字體顏色,不同的背景色呢
這時我們可以去重寫KeyboardView的OnDraw()方法
public class DIYKeyboardView extends KeyboardView {
public DIYKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Keyboard keyboard = getKeyboard();
if (keyboard == null) return;
List.Key> keys = keyboard.getKeys();
if (keys != null && keys.size() > 0) {
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
paint.setTypeface(font);
paint.setAntiAlias(true);
for (Keyboard.Key key : keys) {
if (key.codes[0] == -3) {
Drawable dr = getContext().getResources().getDrawable(R.drawable.keyboard_blue);
dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
dr.draw(canvas);
} else {
Drawable dr = getContext().getResources().getDrawable(R.drawable.keyboard_white);
dr.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
dr.draw(canvas);
}
if (key.label != null) {
if (key.codes[0] == -4 ||
key.codes[0] == -5) {
paint.setTextSize(17 * 2);
} else {
paint.setTextSize(20 * 2);
}
if (key.codes[0] == -4) {
paint.setColor(getContext().getResources().getColor(R.color.white));
} else {
paint.setColor(getContext().getResources().getColor(R.color.blue_03A9F4));
}
Rect rect = new Rect(key.x, key.y, key.x + key.width, key.y + key.height);
Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
int baseline = (rect.bottom + rect.top - fontMetrics.bottom - fontMetrics.top) / 2;
// 下面這行是實現水平居中,drawText對應改為傳入targetRect.centerX()
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(key.label.toString(), rect.centerX(), baseline, paint);
}
}
}
}
}
總結
以上是生活随笔為你收集整理的android keyboard颜色,Android基于KeyboardView和Keyboard实现自定义软键盘 自定义键盘背景色...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: *理解JavaWeb目录结构
- 下一篇: 安装CentOS 5.x与多重引导小技巧