【问题标题】:android image button tooltipandroid图像按钮工具提示
【发布时间】:2014-04-15 15:00:50
【问题描述】:

我正在尝试使用类似于操作栏的图像按钮构建一个应用程序,但我无法让它们在长按时显示工具提示。

   <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 适用于悬停(s-pen),但长按仍然无济于事。

【问题讨论】:

标签: android xml tooltip imagebutton


【解决方案1】:

使用 api 级别 26,您可以使用内置的 TooltipText: 通过 XML:

android:toolTipText="yourText"

ViewCompatDocs

如果您的 minSdk 低于 26,请使用ToolTipCompat,例如:

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

不幸的是,在您的 XML 文件中没有办法做到这一点。

【讨论】:

  • 此工具提示是否仅在长按视图时显示?如果我想在没有用户点击视图的情况下显示工具提示文本,我该怎么办?
【解决方案2】:

这是Support Library v7 用于显示操作菜单项的“备忘单”的代码:

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

【讨论】:

    【解决方案3】:

    在xml中工作并且支持minSdk小于26的解决方案

    @BindingAdapter("tooltipTextCompat")
    fun bindTooltipText(view: View, tooltipText: String) {
        TooltipCompat.setTooltipText(view, tooltipText)
    }
    

    以及layout.xml中的用法

    app:tooltipTextCompat="@{@string/add_for_all_tooltip}"
    

    【讨论】:

    • androidx 理解 app:tooltipText="@string/mystring" 但在运行时它只是不起作用。我需要 XML 和代码吗?
    • 是的。两者都是必需的。
    • 所以 kotlin 函数作用域是全局的?由于 @BindingAdapter 注释,它然后被绑定框架拾取?我还不如只绑定“contentDescription”并在 XML 中为自己节省更多的工作和冗余,对吗?
    • 让它工作最终是缺少我的 XML 中的基本布局元素和对生成的 MyLayoutBinding.inflate 函数的调用。见youtu.be/W7uujFrljW0
    • 此工具提示是否仅在长按视图时显示?如果我想在没有用户点击视图的情况下显示工具提示文本,我该怎么办?
    【解决方案4】:

    不完全是您正在寻找的东西,但它的功能非常相似。

    1) 确保您的视图是android:longClickable="true"(默认情况下应该是)并且具有定义的内容描述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) 注册一个回调,以便在长按视图时调用。处理程序会将内容描述显示为 toast 消息。

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

    【讨论】:

    • 这并不理想,因为它不会显示长按的工具提示,但会显示在标准 Toast 消息位置 (??)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2014-10-09
    相关资源
    最近更新 更多