点击了哪个子视图?

时间:2014-07-12 11:21:30

标签: android events event-bubbling

我有View,其中包含4个视图。我使用方法View.OnClickListener注册了活动setOnClickListener

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

4 个答案:

答案 0 :(得分: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;
        }
    }
}

答案 1 :(得分:1)

您需要为您的视图设置标记并将其设置为可点击。

稍后您可以在此方法中获取view.gettag()的ID

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

答案 2 :(得分: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);

        }
    }

});

答案 3 :(得分:0)

为每个孩子

android:duplicateParentState="true"