Androidがタッチイベントを処理する方法について頭を落とそうとしています。 私が知っているところでは、ビュー要素のonTouch()
イベントがtrueに設定されている場合、要素がそのタッチイベントを処理する準備ができている、つまりタッチイベントを消費していることを意味します。
これは、以下のコードスニペットで実行後ロックがtrueを返したときにラジオグループの選択を変更できなくなった
私はあまりよく知らないのでアンドロイドタッチイベントの仕組み誰かが私のためにそれを正しく要約してください。 onTouch()
のイベントで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());
}
}
活動の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>
このコードスニペットは、https://developer.android.com/reference/android/view/View.OnTouchListener.htmlでドキュメントからCustom Touch Examples