2015-11-18 62 views
-1

我想在我的應用程序中實現OnTouchListener。我想:Android 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; 
     } 
    }); 
+0

您遇到什麼問題? – Kuffs

+0

此代碼無法使用。當我觸摸imageView的linearlayout,我把其他兩個按鈕不會消失 –

+1

如果你只是想隱藏'linearLayout'你爲什麼不使用'onClickListener'? – Wizard

回答

0

代替View.INVISIBLE ==>View.Gone

更新

嘗試沒有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; 
     } 
    }); 

此代碼對我制定各種工作。 希望它有幫助

+0

仍然不能正常工作:( –

0

使用不可見仍然會有物體佔用佈局空間。改爲使用Gone會隱藏對象並移除空間。

affBtn.setVisibility(View.GONE); 
+0

每次觸摸時我都想顯示或隱藏如果我點擊並使可見性消失,當我再次點擊時,如何使其可見? –

+0

設置當你想再次看到它時可見 – AquaMorph

0

你確定你不想要OnClickListener

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

這會使您的視圖在每次點擊之間在可見和不可見之間切換。

如果你想的看法與它佔用的空間一起消失,在代碼片段上面View.GONE

0

,請返回「真」 ACTION_DOWN後更改View.INVISIBLE。然後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; 
    } 
});