【问题标题】:Which child-view was clicked?点击了哪个子视图?
【发布时间】:2014-07-12 11:21:30
【问题描述】:

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

有没有可能知道四个孩子中的哪个被压了?

【问题讨论】:

  • 尝试比较view id和onClick view。

标签: android events event-bubbling


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

ma​​in.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;
        }
    }
}

【讨论】:

  • 这实际上不是一个糟糕的解决方案。那么,您的假设是事件是从下往上冒泡的?
【解决方案2】:

您需要为您的视图设置标签并将它们设置为可点击。

稍后您可以在此方法中通过view.gettag() 获取 id

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

【讨论】:

  • 但事件onClick与父级相关。不是孩子们。
  • @AngryOliver 你会得到被点击的孩子。这就是为什么我提到为您的视图设置标签。
【解决方案3】:

你可以试试这个:

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);

        }
    }

});

【讨论】:

  • 什么是childImageViewA
【解决方案4】:

每个孩子

android:duplicateParentState="true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多