【问题标题】:How to Change The custom keyboard color when pressed ...!如何更改按下时的自定义键盘颜色...!
【发布时间】:2017-02-14 20:07:06
【问题描述】:

嘿,我创建了一个游戏,所以我用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);
}

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>

【问题讨论】:

    标签: android colors background keyboard


    【解决方案1】:

    可能与

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

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 2014-03-11
      • 1970-01-01
      • 2019-08-25
      • 2017-11-04
      • 1970-01-01
      • 2021-02-24
      • 2014-04-07
      • 1970-01-01
      相关资源
      最近更新 更多