如何实现点击某个区域做动作? [英] How to implement click the certain area to do to action?
本文介绍了如何实现点击某个区域做动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<一个href=\"https://github.com/gitgrimbo/android-sliding-menu-demo/blob/master/SlidingMenuDemo/src/grimbo/android/demo/slidingmenu/SlideAnimationThenCallLayout.java\"相对=nofollow>这是Facebook的滑动菜单的一个例子。
This is an example of facebook sliding menu.
滑动时,有20%的空间,用户可以看到哪些是与Facebook相似。与Facebook的点击的这20%的任何地方实现它,菜单是滑回来了。
When sliding, there is 20% space user can see which is similar with facebook. Facebook implements it with clicking anywhere of this 20% and the menu is slide back.
如何实现这一点?
推荐答案
这样做的一个方法是为您的活动与OnTouchListener以下。您可以准确地检测,其中在屏幕上触摸。
One way of doing that is as following with OnTouchListener on your activity. You can detect exactly where is touched on the screen.
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AndroidTestActivity extends Activity implements OnTouchListener {
LinearLayout main;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main = (LinearLayout) findViewById(R.id.main_layout);
main.setOnTouchListener(this); // you need to set the touch listener for your view. And every element around the detection area.
}
public boolean onTouch(View v, MotionEvent e) {
if(e.getX() <= main.getWidth() / 5) {
Toast.makeText(this, "In the %20..", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
您还需要标识主布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
这篇关于如何实现点击某个区域做动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文