没有直接的方法可以使用被调用的方法来做到这一点,Activity
但正如@SD 指出的那样,您可以使用onInterceptTouchEvent
ofViewGroup
来做到这一点:
MyLinearLayout.java
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context) {
super(context);
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(21)
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public boolean onInterceptTouchEvent(final MotionEvent ev) {
Log.e("TOUCHES", "num: " + ev.getPointerCount());
return super.onInterceptTouchEvent(ev);
}
}
我的布局.xml
<mypackage.MyLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:clickable="true"
android:gravity="center"
android:text="0"/>
<TextView
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:clickable="true"
android:gravity="center"
android:text="0"/>
</mypackage.MyLinearLayout>
我的活动.java
public class MapsActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
final TextView t1 = (TextView) findViewById(R.id.t1);
final TextView t2 = (TextView) findViewById(R.id.t2);
t1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent motionEvent) {
t1.setText(""+motionEvent.getPointerCount());
return true;
}
});
t2.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent motionEvent) {
t2.setText(""+motionEvent.getPointerCount());
return true;
}
});
}
}
本例将 t1 和 t2 的文本设置为TextView
每个getPointerCount()
接收MotionEvent
到的文本,并将记录接收到的TextView
总值getPointerCount()
MotionEvent
MyLinearLayout