下面列出了怎么用android.view.View.OnLongClickListener的API类实例代码及写法,或者点击链接到github查看源代码。
public static ViewAction setEndIconOnLongClickListener(
final OnLongClickListener onLongClickListener) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextInputLayout.class);
}
@Override
public String getDescription() {
return "Set end icon OnLongClickListener";
}
@Override
public void perform(UiController uiController, View view) {
TextInputLayout layout = (TextInputLayout) view;
layout.setEndIconOnLongClickListener(onLongClickListener);
}
};
}
public static ViewAction setStartIconOnLongClickListener(
final OnLongClickListener onLongClickListener) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextInputLayout.class);
}
@Override
public String getDescription() {
return "Set a long click listener for the start icon";
}
@Override
public void perform(UiController uiController, View view) {
TextInputLayout layout = (TextInputLayout) view;
layout.setStartIconOnLongClickListener(onLongClickListener);
}
};
}
void showTutorial() {
if ((adapter.getCount() > 0
&& PreferenceManager.getDefaultSharedPreferences(context)
.getBoolean("showVaultLongClickTutorial", true))) {
final View mView =
context.getLayoutInflater().inflate(R.layout.vault_item_tutorial, mLinearView, false);
TextView mInstructions = (TextView) mView.findViewById(R.id.Tutorial__instruction);
if (mInstructions != null)
mInstructions.setText(R.string.Tutorial__long_click_to_rename);
mLinearView.addView(mView, 0);
mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mLinearView.removeView(mView);
SharedPreferences.Editor editor
= PreferenceManager.getDefaultSharedPreferences(context).edit();
editor.putBoolean("showVaultLongClickTutorial", false);
editor.apply();
return true;
}
});
}
}
@Override
public void onDualModeSet(View tileView, boolean enabled) {
if (enabled) {
View bgView = (View) XposedHelpers.getObjectField(tileView, "mTopBackgroundView");
bgView.setOnLongClickListener((OnLongClickListener)
XposedHelpers.getObjectField(tileView, "mLongClick"));
}
}
/**
* Builds an {@link ImageButton} from the data in this params. Generated buttons should be
* placed on the bottom bar. The button's tag will be its id.
* @param parent The parent that the inflated {@link ImageButton}.
* @param listener {@link OnClickListener} that should be used with the button.
* @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid.
*/
ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickListener listener) {
if (mIsOnToolbar) return null;
ImageButton button = (ImageButton) LayoutInflater.from(context)
.inflate(R.layout.custom_tabs_bottombar_item, parent, false);
button.setId(mId);
button.setImageBitmap(mIcon);
button.setContentDescription(mDescription);
if (mPendingIntent == null) {
button.setEnabled(false);
} else {
button.setOnClickListener(listener);
}
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final int screenWidth = view.getResources().getDisplayMetrics().widthPixels;
final int screenHeight = view.getResources().getDisplayMetrics().heightPixels;
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final int width = view.getWidth();
Toast toast = Toast.makeText(
view.getContext(), view.getContentDescription(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.END,
screenWidth - screenPos[0] - width / 2,
screenHeight - screenPos[1]);
toast.show();
return true;
}
});
return button;
}
/**
* Builds an {@link ImageButton} from the data in this params. Generated buttons should be
* placed on the bottom bar. The button's tag will be its id.
* @param parent The parent that the inflated {@link ImageButton}.
* @param listener {@link OnClickListener} that should be used with the button.
* @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid.
*/
ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickListener listener) {
if (mIsOnToolbar) return null;
ImageButton button = (ImageButton) LayoutInflater.from(context)
.inflate(R.layout.custom_tabs_bottombar_item, parent, false);
button.setId(mId);
button.setImageBitmap(mIcon);
button.setContentDescription(mDescription);
if (mPendingIntent == null) {
button.setEnabled(false);
} else {
button.setOnClickListener(listener);
}
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final int screenWidth = view.getResources().getDisplayMetrics().widthPixels;
final int screenHeight = view.getResources().getDisplayMetrics().heightPixels;
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final int width = view.getWidth();
Toast toast = Toast.makeText(
view.getContext(), view.getContentDescription(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.END,
screenWidth - screenPos[0] - width / 2,
screenHeight - screenPos[1]);
toast.show();
return true;
}
});
return button;
}
/**
* Builds an {@link ImageButton} from the data in this params. Generated buttons should be
* placed on the bottom bar. The button's tag will be its id.
* @param parent The parent that the inflated {@link ImageButton}.
* @param listener {@link OnClickListener} that should be used with the button.
* @return Parsed list of {@link CustomButtonParams}, which is empty if the input is invalid.
*/
ImageButton buildBottomBarButton(Context context, ViewGroup parent, OnClickListener listener) {
if (mIsOnToolbar) return null;
ImageButton button = (ImageButton) LayoutInflater.from(context)
.inflate(R.layout.custom_tabs_bottombar_item, parent, false);
button.setId(mId);
button.setImageBitmap(mIcon);
button.setContentDescription(mDescription);
if (mPendingIntent == null) {
button.setEnabled(false);
} else {
button.setOnClickListener(listener);
}
button.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
final int screenWidth = view.getResources().getDisplayMetrics().widthPixels;
final int screenHeight = view.getResources().getDisplayMetrics().heightPixels;
final int[] screenPos = new int[2];
view.getLocationOnScreen(screenPos);
final int width = view.getWidth();
Toast toast = Toast.makeText(
view.getContext(), view.getContentDescription(), Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.END,
screenWidth - screenPos[0] - width / 2,
screenHeight - screenPos[1]);
toast.show();
return true;
}
});
return button;
}
@Override
protected void apply(View view, final TypedArray typedArray) {
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
onAction(typedArray.getString(R.styleable.OnLongClickDecorator_decorOnLongClick));
return true;
}
});
}
/**
* 文本消息
*
* @param message
* @param holder
* @param position
*/
private void handleTextMessage(EMMessage message, ViewHolder holder, final int position) {
TextMessageBody txtBody = (TextMessageBody) message.getBody();
Spannable span = SmileUtils.getSmiledText(context, txtBody.getMessage());
// 设置内容
holder.tv.setText(span, BufferType.SPANNABLE);
// 设置长按事件监听
holder.tv.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
activity.startActivityForResult(
(new Intent(activity, ContextMenu.class)).putExtra("position", position).putExtra("type",
EMMessage.Type.TXT.ordinal()), ChatActivity.REQUEST_CODE_CONTEXT_MENU);
return true;
}
});
if (message.direct == EMMessage.Direct.SEND) {
switch (message.status) {
case SUCCESS: // 发送成功
holder.pb.setVisibility(View.GONE);
holder.staus_iv.setVisibility(View.GONE);
break;
case FAIL: // 发送失败
holder.pb.setVisibility(View.GONE);
holder.staus_iv.setVisibility(View.VISIBLE);
break;
case INPROGRESS: // 发送中
holder.pb.setVisibility(View.VISIBLE);
holder.staus_iv.setVisibility(View.GONE);
break;
default:
// 发送消息
sendMsgInBackground(message, holder);
}
}
}
@Override
public void initialize(TextView widget, final Spannable text) {
widget.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
skipNextClick = true;
Selection.removeSelection(text);
return false;
}
});
super.initialize(widget, text);
}
@Override
public void setOnLongClickListener(OnLongClickListener l) {
if (l == this) {
super.setOnLongClickListener(l);
return;
}
mOnLongClickListener = l;
}
protected void extraMenuSetup () {
final View mnuPost = findViewById(R.id.mnuPost);
mnuPost.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick (final View v) {
askPost(true);
return true;
}
});
}
@Override
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
@Override
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
public OnLongClickListener getOnLongClickListener() {
return mLongClickListener;
}
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
@Override
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
public void setOnLongClickListener(OnLongClickListener listener)
{
mLongClickListener = listener;
}
@Override
public final void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
@Override
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
@Override
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
@Override
public final void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
@Override
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
@Override
public void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}
@Override
public final void setOnLongClickListener(OnLongClickListener listener) {
mLongClickListener = listener;
}