use*_*672 3 java android ontouchlistener touch-event ontouch
我试图完全理解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>
Run Code Online (Sandbox Code Playgroud)
我已经在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;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我读过当我在 中返回 false 时ACTION_DOWN,其余的手势(MOVE和UP)不起作用。但它在此代码中有效,“向上”消息显示在屏幕上,但它不应该显示。所以我完全不明白事件中的返回值是什么意思OnTouch。
有人可以帮助我吗?
| 归档时间: |
|
| 查看次数: |
5272 次 |
| 最近记录: |