Android - 当单选按钮的 ontouch() 方法设置为 true 时会发生什么
Android - What happens when the ontouch() method of a radio button is set to true
我正在努力思考 Android 如何处理触摸事件,对此我感到困惑。
据我所知,如果视图元素的 onTouch()
事件设置为 true,则意味着该元素已准备好处理该触摸事件,即它会消耗触摸事件。
如果是这样,在下面的代码片段中,在 运行 之后,锁在返回 true 时不允许我们更改无线电组的选择
我敢肯定是因为我不太熟悉 android 触摸事件的工作原理。有人可以为我总结一下吗?并解释为什么为 onTouch()
事件返回 true
会阻止我们选择新项目。
package com.examples.customtouch;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
/**
* Created by Dave Smith
* Double Encore, Inc.
* Date: 9/25/12
* TouchListenerActivity
*/
public class TouchListenerActivity extends Activity implements View.OnTouchListener {
/* Views to display last seen touch event */
CheckBox mLockBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch_listener);
mLockBox = (CheckBox) findViewById(R.id.checkbox_lock);
findViewById(R.id.selection_first).setOnTouchListener(this);
findViewById(R.id.selection_second).setOnTouchListener(this);
findViewById(R.id.selection_third).setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
/*
* Consume the events here so the buttons cannot process them
* if the CheckBox in the UI is checked
*/
return mLockBox.isChecked();
}
private String getNameForEvent(MotionEvent event) {
String action = "";
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
action = "ACTION_DOWN";
break;
case MotionEvent.ACTION_CANCEL:
action = "ACTION_CANCEL";
break;
case MotionEvent.ACTION_MOVE:
action = "ACTION_MOVE";
break;
case MotionEvent.ACTION_UP:
action = "ACTION_UP";
break;
default:
return null;
}
return String.format("%s\n%.1f, %.1f", action, event.getX(), event.getY());
}
}
activity 的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<CheckBox
android:id="@+id/checkbox_lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lock Selection" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/selection_first"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="First"/>
<RadioButton
android:id="@+id/selection_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Second"/>
<RadioButton
android:id="@+id/selection_third"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Third"/>
</RadioGroup>
</LinearLayout>
请注意,此代码片段来自 Dave Smith called Custom Touch Examples
的项目
根据 https://developer.android.com/reference/android/view/View.OnTouchListener.html 的文档,returning true
from onTouch
表示 event
已被消耗。虽然我不是 100% 确定,但我的收获是它不应该传播到层次结构中的其他元素,因此事件不会传递到布局中的三个 RadioButton
,这意味着他们不会收到任何触摸通知。
onTouch
方法有两个用途,允许您对该事件执行自定义逻辑,并指示是否通过 return 值将事件传递给其他处理程序。
我正在努力思考 Android 如何处理触摸事件,对此我感到困惑。
据我所知,如果视图元素的 onTouch()
事件设置为 true,则意味着该元素已准备好处理该触摸事件,即它会消耗触摸事件。
如果是这样,在下面的代码片段中,在 运行 之后,锁在返回 true 时不允许我们更改无线电组的选择
我敢肯定是因为我不太熟悉 android 触摸事件的工作原理。有人可以为我总结一下吗?并解释为什么为 onTouch()
事件返回 true
会阻止我们选择新项目。
package com.examples.customtouch;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
/**
* Created by Dave Smith
* Double Encore, Inc.
* Date: 9/25/12
* TouchListenerActivity
*/
public class TouchListenerActivity extends Activity implements View.OnTouchListener {
/* Views to display last seen touch event */
CheckBox mLockBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch_listener);
mLockBox = (CheckBox) findViewById(R.id.checkbox_lock);
findViewById(R.id.selection_first).setOnTouchListener(this);
findViewById(R.id.selection_second).setOnTouchListener(this);
findViewById(R.id.selection_third).setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
/*
* Consume the events here so the buttons cannot process them
* if the CheckBox in the UI is checked
*/
return mLockBox.isChecked();
}
private String getNameForEvent(MotionEvent event) {
String action = "";
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
action = "ACTION_DOWN";
break;
case MotionEvent.ACTION_CANCEL:
action = "ACTION_CANCEL";
break;
case MotionEvent.ACTION_MOVE:
action = "ACTION_MOVE";
break;
case MotionEvent.ACTION_UP:
action = "ACTION_UP";
break;
default:
return null;
}
return String.format("%s\n%.1f, %.1f", action, event.getX(), event.getY());
}
}
activity 的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<CheckBox
android:id="@+id/checkbox_lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lock Selection" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/selection_first"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="First"/>
<RadioButton
android:id="@+id/selection_second"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Second"/>
<RadioButton
android:id="@+id/selection_third"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Third"/>
</RadioGroup>
</LinearLayout>
请注意,此代码片段来自 Dave Smith called Custom Touch Examples
的项目根据 https://developer.android.com/reference/android/view/View.OnTouchListener.html 的文档,returning true
from onTouch
表示 event
已被消耗。虽然我不是 100% 确定,但我的收获是它不应该传播到层次结构中的其他元素,因此事件不会传递到布局中的三个 RadioButton
,这意味着他们不会收到任何触摸通知。
onTouch
方法有两个用途,允许您对该事件执行自定义逻辑,并指示是否通过 return 值将事件传递给其他处理程序。