【发布时间】:2016-10-13 03:03:27
【问题描述】:
我的项目上有一个按钮,并且效果在触摸事件上消失了。我希望当有人触摸该按钮时它应该改变。但 XML 对我没有任何作用。下面是我的代码。 java代码。
btnOn.setOnTouchListener(new View.OnTouchListener() {
//@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mConnectedThread.write("A");
Toast.makeText(getBaseContext(), "LED ON NOW", Toast.LENGTH_SHORT).show();
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
mConnectedThread.write("C");
Toast.makeText(getBaseContext(), "LED SWITCHED OFF", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
下面是 XML 代码。
<Button
android:id="@+id/buttonOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LED ON/OFF"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="148dp"/>
【问题讨论】:
-
我尝试使用选择器和图像按钮stackoverflow.com/questions/7175873/…,但它不起作用。
标签: android button touch effect