Search code examples
androideventsevent-bubbling

Which child-view was clicked?


I have a View which contains 4 views. I registered to the event View.OnClickListener by using the method setOnClickListener.

Is it possible to know which of the four child was pressed?


Solution

  • Try this way,hope this will help you to solve your problem.

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