Android 中多个按钮同时监听的除实现onClickListener方法的另一个方法:
用一个工具类,里面主要实现该方法,如下:public static void setClicks(final View.OnClickListener listener, final View... views) { for (View v : views) { v.setOnClickListener(listener); } }
在具体的监听方法里面,我们这样实现:(后面的参数就是各个被监听的对象)
ViewUtil.setClicks(this, mVAvatar, mVVipTag, mVGrade, mVDiamond, mVDiamondTag, mVBill, mVOrder, mVMyVisitors, mVMyGallery, mVMyGift, mVGiftExchange);
在监听事件中,我们如下实现
@Override public void onClick(View v) { Activity activity = getActivity(); if (v == mVAvatar) { Intent personalInfoIntent = new Intent(activity, PersonalInfoActivity.class); activity.startActivity(personalInfoIntent); } else if (v == mVVipTag) { String url = ServiceUrlConstants.URL.getVipIntroductionUrl(); Map<String, String> queryParameters = new LinkedHashMap<>(); queryParameters.put(UrlConstant.USER_ID, UserSecretInfoUtil.getUserId()); queryParameters.put(UrlConstant.DEVICE_ID, DeviceInfo.getDeviceId(BaseApplication.getContext())); url = UrlUtils.splitJoint(url, queryParameters); Bundle bundle = new Bundle(); bundle.putString(RefreshWebViewActivity.WEB_VIEW_BUNDLE_TITLE, BaseApplication.getContext().getString(R.string.vip_introduction)); bundle.putString(RefreshWebViewActivity.WEB_VIEW_BUNDLE_LOAD_URL, url); Intent intent = new Intent(activity, RefreshWebViewActivity.class); intent.putExtra(RefreshWebViewActivity.WEBVIEW_BUNDLE_NAME, bundle); activity.startActivity(intent); } else if (v == mVGrade) { Intent gradeIntent = new Intent(activity, GradeActivity.class); activity.startActivity(gradeIntent); } }