【问题标题】:Android showing status bar click buttonAndroid显示状态栏点击按钮
【发布时间】:2017-06-27 11:12:11
【问题描述】:

在我的 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);
    }



}

【问题讨论】:

    标签: java android button status


    【解决方案1】:

    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

    【讨论】:

    • 你没看懂这个问题,这里的目的是消除这个bug:在全屏模式下滑动显示状态栏时(状态栏不可见)---> a按钮被点击。现在如果有滑动而不是简单的触摸,如何防止点击
    • 这正是我的回答。您需要“使用”滑动。
    • 没用老兄,我认为问题是当第一次点击是在屏幕的最顶部边缘时,应用程序的 onTouchEvent 没有检测到状态栏,因此第二次点击按钮,被检测为第一次点击(根据应用)
    • 能否提供其他代码。 (布局 xml 和相关的 java 部分)。我尝试做同样的事情(线性布局和全屏活动中的三个水平按钮。)但无法重现您的原始问题。另外我没有使用onTouchEvent 代码。
    • 代码中唯一困扰我的是方法speech 不是静态的。你有没有收到任何警告。 (但我不认为这可能是你的问题的原因)
    猜你喜欢
    • 1970-01-01
    • 2011-09-19
    • 2013-08-22
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-03
    • 2016-01-02
    相关资源
    最近更新 更多