我制作了android和arduino应用程序来控制我的站立桌。我上传的GitHub上(https://github.com/neosarchizo/MyStandingDesk)这个项目按下状态的android选择器不起作用
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/down_button_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/down_button" />
</selector>
我想在MainActivity按钮使用选择。如果我按下某个按钮,则必须更改该按钮的图像。但它没有奏效。所以我通过检查MotionEvent以编程方式更改按钮的图像。
btnDown.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
btnDown.setImageResource(R.drawable.down_button_pressed);
sendCommand("a");
break;
case MotionEvent.ACTION_UP:
btnDown.setImageResource(R.drawable.down_button);
sendCommand("s");
break;
}
return true;
}
});
此外,我在布局xml文件中为选择器编写了以下代码。
<ImageButton
android:id="@+id/btnDown"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_margin="30dip"
android:layout_weight="1"
android:scaleType="fitCenter"
android:background="@android:color/transparent"
android:src="@drawable/selector_down_button" />
我已经试过了。但它也没有工作。请克隆我的项目并进行测试。 – neosarchizo
现在检查我更新的答案我已经测试it.it的工作完美。 –
我可以解决这个问题没有'view.setPressed()'? – neosarchizo