Android显示状态栏单击按钮

时间:2017-06-27 11:12:11

标签: java android button status

在我的Android应用程序中,我有3个全屏幕ImageButtons(水平在LinearLayout内部),我隐藏了状态栏(全屏应用程序)

问题在于,当我向下滑动以显示状态栏时,顶部的ImageButton被点击,我尝试检测点击次数,因此我禁用了按钮的功能,如果它是滑动而不是像这样的点击

@Override
public boolean onTouchEvent(MotionEvent event)
{
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN :
            y1 = event.getY();

            i=0;

            break;
        case MotionEvent.ACTION_UP:
            y2 = event.getY();

            float deltaY = y2-y1; 
            if(deltaY < 10 && deltaY > -10)
            {
                flag=true;
            }
            else
            {
                flag=false;
            }
            break;
    }
    return super.onTouchEvent(event);
}

但它似乎不起作用。

非常感谢任何帮助,谢谢你。

编辑1:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:orientation="vertical"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#567"
    >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="?android:attr/buttonStyleSmall"
        android:background="?android:attr/selectableItemBackground"
        android:onClick="speech"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#456"
    >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        style="?android:attr/buttonStyleSmall"
        android:onClick="pics"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:background="#345"
    >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?android:attr/selectableItemBackground"
        style="?android:attr/buttonStyleSmall"
        android:id="@+id/vids"
        />
</LinearLayout>
</LinearLayout>

Java:

public class BirthdayActivity extends AppCompatActivity {

    private float x1,x2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_birthday);


    }

    public void speech(View v)
    {

        finish();
        Intent intent = new Intent(BirthdayActivity.this , SpeechActivity.class);
        BirthdayActivity.this.startActivity(intent);
    }

    public void pics(View v)
    {
        finish();
        Intent intent = new Intent(BirthdayActivity.this , PicsActivity.class);
        BirthdayActivity.this.startActivity(intent);
    }





    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN :
                x1 = event.getY();

                break;
            case MotionEvent.ACTION_UP:
                x2 = event.getY();

                float Delta = x2-x1;

                //if on actionUp, the distance is big == swipe 
                if(Delta < 5 && Delta(-5))
                {

                }
                else
                {

                }
                break;
        }
        return super.onTouchEvent(event);
    }



}

1 个答案:

答案 0 :(得分:0)

  

onTouchEvent如果您使用了此操作则返回true,如果您没有,则返回false

所以,你应该这样 -

@Override
public boolean onTouchEvent(MotionEvent event)
{
    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN :{
            //something
            return true;
        }
        case MotionEvent.ACTION_UP: {
            //something else
            return true;
        }
    }
    //some other event, let system handle it
    return super.onTouchEvent(event);
}

docs