嘿我有创建游戏所以我创建了带有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>
答案 0 :(得分:0)
可能与
<item android:state_checked="true"
android:drawable="@drawable/pressed"/>