2014-09-04 21 views
0

這個問題是相當愚蠢的問,但出,但我想知道它是如何工作?相對佈局所有孩子可點擊

我有一個Relative佈局與2 ImageViews作爲孩子有單獨的clickListner實例。一個大小和屬性相同的另一個。

重疊對方。兩個都有不同的圖像。 問題是當我點擊一個圖像ImageView點擊列表被調用。

或者,如果我禁用點擊ImageView頂部,下面的ImageView仍然有效,我點擊圖片視圖上方雖然。它是怎麼回事從兩者獲得回調。

我只想知道它是如何工作的?不是代碼,我沒有任何問題編寫代碼clickListners是否只有一個Working或Both。

<RelativeLayout 

---- 
--- 
> 
<ImageView 
--- 
---<!--Child 1--> 
<ImageView 
--- 
---<!--Child 2--> 



<RelativeLayout/> 
+0

**相關的**代碼在哪裏? – 2014-09-04 08:07:32

回答

0

here摘自: 如果你只用一個clicklistener工作,你可以這樣做:

View.OnClickListener myOnlyhandler = new View.OnClickListener() { 
    public void onClick(View v) { 
     switch(v.getId()) { 
     case R.id.b1: 
      // it was the first button 
      break; 
     case R.id.b2: 
      // it was the second button 
      break; 
     } 
    } 
} 
0

使用的ImageButton

佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<ImageButton 
    android:id="@+id/imageButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:contentDescription="@string/content" 
    android:src="@drawable/ic_launcher" /> 

</LinearLayout> 

Activity類

public class MainActivity extends Activity { 

ImageButton imgButton; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    addButtonListener(); 
} 

public void addButtonListener() { 

    imgButton = (ImageButton) findViewById(R.id.imageButton); 
    imgButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(MainActivity.this,"ImageButton is working!", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 
} 
0

把這個給你的父母佈局 機器人:上下文=「Yourclasshere」 然後把這個給你的圖像視圖 安卓的onclick =「點擊」

,然後實現上點擊收聽或使像Vitly A的方法制作