我正在尝试在按钮按下时更改布局的背景颜色,但在发布时更改回原始可绘制对象。
以下代码更改了背景资源 onclick 按钮,该按钮更改了背景,但在释放时它与新的可绘制资源保持一致:
btnOne = (Button) findViewById(R.id.btnIcon1);
btnOne.setOnClickListener(oneClick);
btnOne.setOnTouchListener(oneC);
View.OnClickListener oneClick = new View.OnClickListener() {
@SuppressLint("NewApi")
public void onClick(View v) {
editor.putString("AndroidInfo", "1");
editor.commit();
Intent myIntent = new Intent(getApplicationContext(), VersionDetail.class);
startActivityForResult(myIntent, 0);
overridePendingTransition(R.anim.right_slide_in, R.anim.right_slide_out);
}
};
View.OnTouchListener oneC = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
rlOne.setBackgroundResource(R.drawable.dateborderclick);
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
rlOne.setBackgroundResource(R.drawable.dateborder);
return true; // if you want to handle the touch event
}
return false;
}
};
日期边框xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<solid android:color="#CCE5E5E5" />
</shape>
dateborderclick xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="4dp"
android:topRightRadius="4dp"
android:bottomRightRadius="4dp"
android:bottomLeftRadius="4dp" />
<stroke
android:width="1dp"
android:color="#FFFFFF" />
<solid android:color="#CCD1FFFE" />
</shape>
dateborder 是默认背景。当用户与按钮交互时,我希望 dateborder 在按下时为 dateborderclick 并在发布时返回 dateborder 。根据上面的代码,它应该可以工作,但现在按下可以工作,但点击不行。