Java 在android中仅检测长按特定线性布局?
我想检测具有特定布局的onLongPress事件。在我的xml LinearLayout中,使用RelativeLayout。我只想在LinearLayout中获得onLongTouch。我尝试使用onTouchEvent,这很好,但onLongPress处理孔布局,包括RelativeLayout。如果我放置Java 在android中仅检测长按特定线性布局?,java,android,xml,android-layout,Java,Android,Xml,Android Layout,我想检测具有特定布局的onLongPress事件。在我的xml LinearLayout中,使用RelativeLayout。我只想在LinearLayout中获得onLongTouch。我尝试使用onTouchEvent,这很好,但onLongPress处理孔布局,包括RelativeLayout。如果我放置返回mDetector.onTouchEvent(事件)在myLinear.setOnTouchListener中使用。它将变成单触式。请帮助我解决此问题。任何回复我都非常感谢 activ
返回mDetector.onTouchEvent(事件)代码>在myLinear.setOnTouchListener中使用。它将变成单触式。请帮助我解决此问题。任何回复我都非常感谢
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:text="Show Text"
android:textStyle="bold"
android:textSize="@dimen/abc_text_size_title_material"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="@+id/myLinear"
android:layout_below="@+id/txtView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/floorimg">
</LinearLayout>
您可能希望与线性布局一起使用:
myLinear.setOnLongClickListener(新视图.OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图v){
Toast.makeText(getBaseContext(),“长按调用”,Toast.LENGTH_SHORT.show();
返回true;
}
});
Bro正是我想要放大线性布局并添加多个pinmark。所以我问了这个问题。我已经试过了。首先,我们得到x和y坐标,然后加上细记号。onLongClickListener只能在布局上调用一次。是否可以引用其他选项?
public class MainActivity extends AppCompatActivity {
LinearLayout myLinear;
private GestureDetectorCompat mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myLinear = (LinearLayout)findViewById(R.id.myLinear);
mDetector = new GestureDetectorCompat(this, new MyGestureListener());
myLinear.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent(event);
}
});
}
/* @Override
public boolean onTouchEvent(MotionEvent ev) {
super.onTouchEvent(ev);
return mDetector.onTouchEvent(ev);
}*/
private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(getBaseContext(),"Long press called",Toast.LENGTH_SHORT).show();
super.onLongPress(e);
}
}
}