How to implement click the certain area to do to action?

早过忘川 提交于 2019-12-11 11:10:42

问题


This is an example of facebook sliding menu.

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.

How to implement this?


回答1:


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;
  }
}

You also need to id your main layout

<?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" >


来源:https://stackoverflow.com/questions/10700362/how-to-implement-click-the-certain-area-to-do-to-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!