我正在努力全面理解OnTouchListener
,但我有一些疑问。
我有以下xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.pablo.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
我已经在Java中实现了这段代码:
public class MainActivity extends Activity {
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.button1);
b.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(getBaseContext(), "boton down",Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(getBaseContext(), "boton up",Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
}
我阅读了一些资料发现,如果在ACTION_DOWN
中返回false,则其他手势(MOVE
和UP
)将不起作用。但是在这段代码中,屏幕上显示了“up”消息,但实际上它不应该出现。所以我不完全理解OnTouch
事件中的返回值的含义。
有人可以帮助我吗?