多个文本视图或点击跨越一个文本视图? [英] multiple text views or one text view with clickable spans?

查看:137
本文介绍了多个文本视图或点击跨越一个文本视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型阵列中的每个重新$ P $文本psenting一个字。它看起来是这样的:

I have an array of models each representing a word in a text. It looks like this:

ArrayList<Word> text;

class Word {
    public String name;
    public boolean isPunctuationMark;

我需要渲染的活动里面的文本。我还需要做的就是当一个字是长期点击我需要触发和事件,并显示具体的选字信息的对话框。

I need to render that text inside an activity. What I also need to do is when a word is long-clicked I need to trigger and event and show a dialog with information specific to the selected word.

有,我可以用两种方法:

There are two approaches that I can use:

  1. 使用一个的TextView 每字部件
  2. 使用一个的TextView 来容纳整个文本,但使每个字 ClickableSpan
  1. use one TextView widget per word
  2. use one TextView to hold entire text but make each word a ClickableSpan

我已经开始实施第二个方法,但存在对 longClick 上点击跨度事件没有原生支持,所以我必须实现它自己。此外,它似乎乍一看是突出文本和选择单词的背景更容易 TextViews 做的,而不是用 ClickableSpans 。所以现在我想尝试第一种方法。是不是有什么探索这些方法时,我已经错过了?也许还有第三更构架正确的选项是更容易实现支持长期的点击?

I've started implementing the second approach but there is no native support for longClick event on clickable spans, so I'd have to implement it myself. Also, it seems at the first glance that highlighting text and background of selected words is easier done with TextViews rather than with ClickableSpans. So now I'm thinking about trying out the first approach. Is there something I've missed when exploring these approaches? Maybe there's a third more architecturally correct option that is easier to implement supporting long-clicks?

推荐答案

我会建议你使用一个的TextView ,因为这是一种更有效的用户界面。当有一个的ViewGroup 来衡量所有它的孩子,它的速度要快得多只有一个衡量比几千。

I would suggest you use one TextView as this is a much more efficient UI. When there is a request for a ViewGroup to measure all of it's children, it is much faster to measure only one than a couple of thousand.

我已经找到了一个很好的图书馆,你可以尝试这里使用。它使用<一个href="http://developer.android.com/reference/android/widget/TextView.html#getOffsetForPosition(float,%20float)"相对=nofollow> getOffsetForPostion 方法在API级别14加入的TextView

I have found a nice library you may try to use here. It uses getOffsetForPostion method added to TextView in API level 14.

公众诠释getOffsetForPosition(浮X,浮动Y)

public int getOffsetForPosition (float x, float y)

加在API级别14使用的字符偏移最接近   指定的绝对位置。一个典型的用例是通过结果   的getX()和getY()以这种方法。

Added in API level 14 Get the character offset closest to the specified absolute position. A typical use case is to pass the result of getX() and getY() to this method.

参数x在屏幕上点的水平绝对位置   Y A点在屏幕上的垂直绝对位置返回   字符中的位置位移字符最接近   指定的位置。返回-1如果没有布局

Parameters x The horizontal absolute position of a point on screen y The vertical absolute position of a point on screen Returns the character offset for the character whose position is closest to the specified position. Returns -1 if there is no layout.

这篇关于多个文本视图或点击跨越一个文本视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆