我想找來創建,其中一個按鈕的初始按下設置的其他3個按鈕從去可見的可見性的效果結束的圖(按鈕)。在向最近可見的按鈕之一滑動並結束後,我想啓動其他活動。
我的問題是,不管方法我用得到的意見(使用矩形對象,findNearestTouchable)和位置觸摸事件(使用ACTION_UP onTouch /手勢)的位置,他們似乎從來沒有匹配,我永遠不能返回我想要的視圖;
期望的功能
,其從按鈕的按壓開始,並且在另一個按鈕結束時,返回按鈕的ID /視圖對象則結束對一個運動事件。
我main_menu.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="Button"
android:tag="1"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_marginBottom="36dp"
android:layout_marginRight="19dp"
android:layout_toLeftOf="@+id/button1"
android:text="Button"
android:visibility="gone"
android:tag="3" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_toRightOf="@+id/button3"
android:text="Button"
android:visibility="gone"
android:tag="2"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:text="Button"
android:visibility="gone"
android:tag="4" />
</RelativeLayout>
在活動現狀的企圖,不設置foundit
(我很開放的,完全不同的實現)
public class Menu extends Activity {
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
View findViewById2 = findViewById(R.id.button2);
View findViewById3 = findViewById(R.id.button3);
View findViewById4 = findViewById(R.id.button4);
findViewById2.setVisibility(View.VISIBLE);
findViewById3.setVisibility(View.VISIBLE);
findViewById4.setVisibility(View.VISIBLE);
return gestureDetector.onTouchEvent(event);
}
};
View button = findViewById(R.id.button1);
button.setOnTouchListener(gestureListener);
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
View foundit = null;
ViewGroup menu = (ViewGroup) findViewById(R.id.button1).getParent();
for (int numChildren = menu.getChildCount(); numChildren >= 0; --numChildren) {
View child = menu.getChildAt(numChildren - 1);
if (child instanceof Button) {
Rect bounds = new Rect();
child.getHitRect(bounds);
if (bounds.contains((int) e2.getX(), (int) e2.getY())) {
foundit = child;
}
}
}
if (foundit != null) {
Log.i("found", "FOUND IT");
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
}
有沒有人有任何見解?
編輯:
測試如下:
gestureListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
View findViewById2 = findViewById(R.id.button2);
View findViewById3 = findViewById(R.id.button3);
View findViewById4 = findViewById(R.id.button4);
findViewById2.setVisibility(View.VISIBLE);
findViewById3.setVisibility(View.VISIBLE);
findViewById4.setVisibility(View.VISIBLE);
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.i("TOUCH X IS", Float.toString(event.getRawX()));
Log.i("TOUCH Y IS", Float.toString(event.getRawY()));
}
for (int numChildren = ((ViewGroup) v.getParent())
.getChildCount(); numChildren >= 0; --numChildren) {
View child = ((ViewGroup) v.getParent())
.getChildAt(numChildren - 1);
if (child instanceof Button) {
Rect bounds = new Rect();
child.getHitRect(bounds);
Log.i(child.getTag().toString() + " CENTER X IS",
Float.toString(bounds.exactCenterX()));
Log.i(child.getTag().toString() + " CENTER Y IS",
Float.toString(bounds.exactCenterY()));
}
}
return false;
}
};
產生以下logcat的輸出:
07-06 01:12:58.245: TOUCH X IS(20965): 394.0
07-06 01:12:58.245: TOUCH Y IS(20965): 297.0
07-06 01:12:58.245: 4 CENTER X IS(20965): 392.0
07-06 01:12:58.245: 4 CENTER Y IS(20965): 219.0
07-06 01:12:58.245: 2 CENTER X IS(20965): 240.0
07-06 01:12:58.250: 2 CENTER Y IS(20965): 219.0
07-06 01:12:58.250: 3 CENTER X IS(20965): 95.0
07-06 01:12:58.250: 3 CENTER Y IS(20965): 219.0
07-06 01:12:58.250: 1 CENTER X IS(20965): 240.0
07-06 01:12:58.250: 1 CENTER Y IS(20965): 345.0
感謝您回答一個非常古老的問題,從我完成任何android開發已經很長時間了,但這使我看看這是否有效。 – Dnlhoust 2017-05-21 14:46:30