Android 如何在按下时更改自定义键盘颜色。。。!

Android 如何在按下时更改自定义键盘颜色。。。!,android,colors,background,keyboard,Android,Colors,Background,Keyboard,嘿,我已经创建了一个游戏,所以我用edit tex创建了自定义键盘(numPad),我可以在编辑文本上写一次数字(第一个数字出现一次,直到我将其退格,然后我可以再写一次),所以我想更改键盘的颜色我尝试了一个代码,按下时它会将按键更改为蓝色,但我想保持蓝色,例如,我按下了数字(1234)键盘上的数字将颜色更改为蓝色,并一直保持这种状态,直到我将此数字退格为其原始颜色(未压缩的颜色) 看看我的代码和图片,你会明白的 MainActivty.java private CustomKeybo

嘿,我已经创建了一个游戏,所以我用edit tex创建了自定义键盘(numPad),我可以在编辑文本上写一次数字(第一个数字出现一次,直到我将其退格,然后我可以再写一次),所以我想更改键盘的颜色我尝试了一个代码,按下时它会将按键更改为蓝色,但我想保持蓝色,例如,我按下了数字(1234)键盘上的数字将颜色更改为蓝色,并一直保持这种状态,直到我将此数字退格为其原始颜色(未压缩的颜色) 看看我的代码和图片,你会明白的

MainActivty.java

    private CustomKeyboardView mKeyboardView;
private EditText mTargetView;
private Keyboard mKeyboard;
String textContainer = "";
int length = 0;
int prevLength = 0;

@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.setPreviewEnabled(false);

    mKeyboardView.setOnKeyboardActionListener(new BasicOnKeyboardActionListener(
                    this));



    mTargetView.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            textContainer = s.toString();
            prevLength = s.length();
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
        @Override
        public void afterTextChanged(Editable s) {
            length = s.length();
                            if(!textContainer.isEmpty()){
                                if(s.length() > 1) {
                                    if(prevLength < length) {
                                        if (!textContainer.contains(s.toString().subSequence(length - 1, length))) {
                                            length = s.length();
                                        } else {
                            mTargetView.getText().delete(length - 1, length);
                        }
                    }
                }
            }else{
                textContainer = s.toString();
            }


        }
    });

}


private void showKeyboardWithAnimation() {
    if (mKeyboardView.getVisibility() == View.GONE) {
        Animation animation = AnimationUtils
                .loadAnimation(KeyboardWidgetActivity.this,
                        R.anim.slide_in_bottom);
        mKeyboardView.showWithAnimation(animation);
    }
}



@Override
public void onBackPressed() {
    if (mKeyboardView.isShown()) {
        mKeyboardView.setVisibility(View.GONE);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    mKeyboardView.setVisibility(View.GONE);
    return    super.onTouchEvent(event);
}
private CustomKeyboardView mKeyboardView;
私有编辑文本视图;
专用键盘键盘;
字符串textContainer=“”;
整数长度=0;
int-prevLength=0;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mKeyboard=新键盘(this,R.xml.Keyboard);
mTargetView=(EditText)findViewById(R.id.target);
mTargetView.setOnTouchListener(新视图.OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
showKeyboardWithAnimation();
返回true;
}
});
mKeyboardView=(CustomKeyboardView)findViewById(R.id.keyboard_视图);
设置键盘(mKeyboard);
mKeyboardView.setPreviewEnabled(false);
mKeyboardView.setOnKeyboardActionListener(新BasicOnKeyboardActionListener(
这个),;
mTargetView.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
textContainer=s.toString();
prevLength=s.length();
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
@凌驾
公共无效后文本已更改(可编辑){
长度=s.长度();
如果(!textContainer.isEmpty()){
如果(s.长度()>1){
如果(前置长度<长度){
如果(!textContainer.contains(s.toString().子序列(长度-1,长度))){
长度=s.长度();
}否则{
mTargetView.getText().delete(长度-1,长度);
}
}
}
}否则{
textContainer=s.toString();
}
}
});
}
私有void showKeyboardWithAnimation(){
if(mKeyboardView.getVisibility()==View.GONE){
Animation=AnimationUtils
.loadAnimation(键盘WidgetActivity.this,
R.anim.滑动(在底部);
mKeyboardView.showWithAnimation(动画);
}
}
@凌驾
public void onBackPressed(){
if(mKeyboardView.isShown()){
mKeyboardView.setVisibility(View.GONE);
}否则{
super.onBackPressed();
}
}
@凌驾
公共布尔onTouchEvent(运动事件){
mKeyboardView.setVisibility(View.GONE);
返回super.onTouchEvent(事件);
}
main.xml

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

<LinearLayout android:layout_width="fill_parent"
    android:id="@+id/container"

    android:layout_alignParentTop="true"
    android:layout_height="fill_parent">
    <EditText
        android:layout_width="fill_parent"

        android:id="@+id/target"
        android:layout_height="wrap_content" />

</LinearLayout>

<com.example.mike.a12345.CustomKeyboardView
    android:id="@+id/keyboard_view" android:visibility="gone"

    android:keyBackground="@drawable/samplekeybackground"


    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ></com.example.mike.a12345.CustomKeyboardView>

samplekeybackround.xml

    <!-- Pressed state -->
    <item
    android:state_pressed="true"
    android:drawable="@drawable/pressed" /></selector>


pressed.xml

   <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="4dp" android:color="#A3D9F3" />
    <solid android:color="#4ABCE8"/>
   </shape>

可能与

<item android:state_checked="true"
      android:drawable="@drawable/pressed"/>