繁体   English   中英

如何更改按下时的自定义键盘颜色......!

[英]How to Change The custom keyboard color when pressed …!

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

空的EditText 当我按1号想要留蓝色工具我退格吧

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

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"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM