2014-07-12 106 views
1

我有一個View其中包含4個意見。我通過使用方法setOnClickListener註冊了事件View.OnClickListener哪個子視圖被點擊?

是否有可能知道四個孩子中的哪一個被按下?

+0

嘗試比較視圖id用的onClick視圖。 –

回答

1

試試這個辦法,希望這將幫助你解決你的問題。

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:gravity="center"> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button1"/> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:text="Button2"/> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:text="Button3"/> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="5dp" 
     android:text="Button4"/> 
</LinearLayout> 

MyActivity.java

public class MyActivity extends Activity implements View.OnClickListener { 


    private Button button1; 
    private Button button2; 
    private Button button3; 
    private Button button4; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     button1 = (Button) findViewById(R.id.button1); 
     button2 = (Button) findViewById(R.id.button2); 
     button3 = (Button) findViewById(R.id.button3); 
     button4 = (Button) findViewById(R.id.button4); 

     button1.setOnClickListener(this); 
     button2.setOnClickListener(this); 
     button3.setOnClickListener(this); 
     button4.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()){ 
      case R.id.button1: 
       Toast.makeText(this,"Button 1 click",Toast.LENGTH_SHORT).show(); 
       break; 
      case R.id.button2: 
       Toast.makeText(this,"Button 2 click",Toast.LENGTH_SHORT).show(); 
       break; 
      case R.id.button3: 
       Toast.makeText(this,"Button 3 click",Toast.LENGTH_SHORT).show(); 
       break; 
      case R.id.button4: 
       Toast.makeText(this,"Button 4 click",Toast.LENGTH_SHORT).show(); 
       break; 
      default: 
       break; 
     } 
    } 
} 
+0

這實際上不是一個不好的解決方案。那麼,你的假設是事件是從下跌冒泡的? – AngryOliver

0

你可以試試這個:

parent.setOnClickListener(new OnClickListener(){ 

    @Override 
    public void onClick(View v) { 
    ViewGroup parent = (ViewGroup)v; 

     for(int index = 0; index < parent.getChildCount(); index++){ 
      View child = parent.getChildAt(index); 

     } 
    } 

}); 
+0

什麼是'childImageViewA'? – AngryOliver

+0

@AngryOliver編輯! – SuppressWarnings

1

您需要標記設置你的意見,並將其設爲可點擊。

以後你可以通過view.gettag()獲得ID這種方法

public void onClick(View v){ 
int id = v.getTag(); 
    switch(id){ 
     case 0: 
     break; 
     default: 
     break; 
    } 
} 
+0

但「onClick」事件與父項相關。不是孩子。 – AngryOliver

+0

@AngryOliver你會得到被點擊的孩子。這就是爲什麼我提到設置標籤到您的視圖。 – CodeWarrior

0

內爲每一個孩子

android:duplicateParentState="true"