簡體   English   中英

Android OnTouchListener

[英]Android OnTouchListener

我想在我的應用程序中實現 OnTouchListener。 我想要的是:

  • 當我觸摸屏幕時,我需要顯示或隱藏一些動作:在這里,當我觸摸 ImageView 時,顯示處理動作的 LinearLayout

我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_af"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">

<ImageView
    android:id="@+id/ivaf"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_centerInParent="true" />

<LinearLayout
    android:id="@+id/llaffbtn"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000"
        android:src="@drawable/ic_file_download_white_24dp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000"
        android:src="@drawable/ic_share_white_24dp" />

</LinearLayout>

以及我在 Java 類中嘗試的內容:

    img = (ImageView) findViewById(R.id.ivaf);
    rl = (RelativeLayout) findViewById(R.id.rl_af);
    affBtn = (LinearLayout) findViewById(R.id.llaffbtn);
    rl.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                affBtn.setVisibility(View.VISIBLE);
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                affBtn.setVisibility(View.INVISIBLE);
            }

            return false;
        }
    });

而不是View.INVISIBLE ==> View.Gone

更新

在沒有MotionEvent.ACTION_DOWN) {...情況下嘗試MotionEvent.ACTION_DOWN) {...像:

    img = (ImageView) findViewById(R.id.ivaf);
    rl= (RelativeLayout) findViewById(R.id.rl_af);
    affBtn = (LinearLayout) findViewById(R.id.llaffbtn);
    rl.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            affBtn.setVisibility(affBtn.getVisibility()== View.VISIBLE ? View.GONE : View.VISIBLE);
            return false;
        }
    });

這段代碼適用於我的設計。 希望能幫助到你

使用 invisible 仍然會使對象在布局中占用空間。 改用 Gone 將隱藏對象並刪除空間。

affBtn.setVisibility(View.GONE);

你確定你真的不想要一個OnClickListener嗎?

rl.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        affBtn.setVisibility(affBtn.getVisibility == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
        }
    }
});

這將使您的視圖在每次單擊時在可見和不可見之間切換。

如果您希望視圖與其占用的空間一起消失, View.INVISIBLE上面代碼片段中的View.GONE更改為View.GONE

嘗試在 ACTION_DOWN 后返回“True”。 然后 Android 會監聽 ACTION_UP 事件並在它發生時調用它。 我在我早期的項目中遇到了類似的問題。

rl.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            affBtn.setVisibility(View.VISIBLE);
            return true;
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            affBtn.setVisibility(View.INVISIBLE);
        }

        return false;
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM