android image button tooltip

匿名 (未验证) 提交于 2019-12-03 08:41:19

问题:

I'm trying to build an app with image buttons that work like the action bar but i can't get them to show a tooltip on long press.

   <ImageButton         android:id="@+id/editUrgent"         style="?android:attr/borderlessButtonStyle"         android:layout_width="48dp"         android:layout_height="wrap_content"         android:layout_centerVertical="true"         android:layout_toLeftOf="@+id/editImportant"         android:hint="@string/hint_urgent"         android:contentDescription="@string/hint_urgent"         android:text="@string/hint_urgent"         android:src="@drawable/clock_light" /> 

android:contentDescription works for hovering(s-pen) but long press still does nothing.

回答1:

This is the code that Support Library v7 uses to show "cheat sheet"s for action menu items:

public boolean onLongClick(View v) {     if (hasText()) {         // Don't show the cheat sheet for items that already show text.         return false;     }      final int[] screenPos = new int[2];     final Rect displayFrame = new Rect();     getLocationOnScreen(screenPos);     getWindowVisibleDisplayFrame(displayFrame);      final Context context = getContext();     final int width = getWidth();     final int height = getHeight();     final int midy = screenPos[1] + height / 2;     int referenceX = screenPos[0] + width / 2;     if (ViewCompat.getLayoutDirection(v) == ViewCompat.LAYOUT_DIRECTION_LTR) {         final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;         referenceX = screenWidth - referenceX; // mirror     }     Toast cheatSheet = Toast.makeText(context, mItemData.getTitle(), Toast.LENGTH_SHORT);     if (midy < displayFrame.height()) {         // Show along the top; follow action buttons         cheatSheet.setGravity(Gravity.TOP | GravityCompat.END, referenceX, height);     } else {         // Show along the bottom center         cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);     }     cheatSheet.show();     return true; } 


回答2:

With api level 26 you can use the built in TooltipText: XML:

android:toolTipText="yourText" 

ViewCompatDocs

For older api levels use ToolTipCompat, for example:

TooltipCompat.setTooltipText(yourView, "your String"); 


回答3:

Not exactly what you are looking for but it does something very similar.

1) Insure your view is android:longClickable="true" (should be by default) and has a defined content description android:contentDescription="@string/myText":

<ImageButton android:id="@+id/button_edit"      android:contentDescription="@string/myText"      android:src="@drawable/ic_action_edit"      android:onClick="onEdit"      android:longClickable="true"/> 

2) Register a callback to be invoked when the view is long pressed. Handler will display the content description as a toast message.

findViewById(R.id.button_edit).setOnLongClickListener(new View.OnLongClickListener() {              @Override             public boolean onLongClick(View view) {                 Toast.makeText(context,view.getContentDescription(), Toast.LENGTH_SHORT).show();                 return true;             }         }); 


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