How is it possible to know which View has clicked in a layout OnClickListener?

霸气de小男生 提交于 2019-12-03 21:01:35

Other answers are quite abstract and some are incorrect. You can't Override onClick method for an Activity as it doesn't have one (unless of course you implement an OnClickListener).

Furthermore, if you set the listener for the layout the View argument received by the onClick method will always be the layout.


You can either create your own custom layout that intercepts the clicks and check if you clicked on a view, or you can set listeners for all the views.

A somewhat cleaner solution is using a single listener and checking if it's a view that was clicked.

private final String TAG = "MainActivity";
List<View> views = new ArrayList<View>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
    TextView tv1 = (TextView) findViewById(R.id.tv1);
    TextView tv2 = (TextView) findViewById(R.id.tv2);
    TextView tv3 = (TextView) findViewById(R.id.tv3);

    views.add(tv1);
    views.add(tv2);
    views.add(tv3);

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isView = false;
            for (View view : views) {
                if (v.equals(view)) {
                    isView = true;
                    break;
                }
            }

            if (isView) {
                Log.e(TAG, "Click on view");
            } else {
                Log.e(TAG, "Click on layout");
            }
        }
    };

    tv1.setOnClickListener(clickListener);
    tv2.setOnClickListener(clickListener);
    tv3.setOnClickListener(clickListener);
    rLayout.setOnClickListener(clickListener);
}

If you want different actions for different views, just create a listener for each of them.

For example Layout has two views View1 and View2. I assume that you have already inflated them. So you have to set OnClickListiner for each of them

Layout.setOnClickListener(this);
View1.setOnClickListener(this);
View2.setOnClickListener(this);

Then you have to override method:

public void onClick(View v) 
    {
        switch(v.getId())
        {
            case R.id.id_for_layout:
                // do what you want when user click layout
                break;

            case R.id.id_for_first_view:
                // do what you want when user click first view
                break;

            case R.id.id_for_second_view:
                // do what you want when user click second view
                break;
          }
}

Remember that the method OnClick have one parameter that indicates which View is clicked!

public void onClick(View v) {
      switch(v.getId()) {
        case R.id.btn1:
          // first button
          break;
        case R.id.btn2:
          // second button
          break;
      }
  }

Assign the id to each of your views by android:id="@+id/v1" then in

onClick(View v)

check for the view like

if(v == findViewById(R.id.v1))
{
 // v1 specific action
}
else if(v == findViewById(R.id.v2))
    {
     // v2 specific action
    }

Ok, it's easy.
You have to add an OnClickListener to each view (button, textView and so on), and to the whole layout as well.
In this case, the onClickListeners that will be launched if you click are the view's onClickListener and the layout's onClickListener.
So no need for a whole bunch of classes.
One will be enough if you try this (do actions depending on the id and don't forget the layout ;) ):

public void onClick(View v) {
  switch(v.getId()) {
    case R.id.layout:
      layout actions here
      break;
    case R.id.textview:
      textview actions here
      break;

this onClickListener is for all of your views ;)

There is View v parameter in onClickListner(View v) method : 


@Override
protected void onCreate(Bundle savedInstanceState) {
    LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
    layout.setOnclickListener(this);

}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.linearLayout){
        //your logic
    } 
}

for anyone interested you can also log the currently clicked view:

 getActivity()
      .getWindow()
      .getDecorView()
      .findViewById(android.R.id.content)
      .setOnTouchListener((v, event) -> {

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    Log.v("[onClick]", v.getClass().getCanonicalName());
                }

                return false;
            });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!