如何在textview Android上找到单击的单词

时间:2014-02-14 19:19:44

标签: android android-layout textview onclicklistener

我有一个令牌列表,用户需要快速创建这些令牌的列表。我现在有键盘输入和列表显示,但键盘覆盖了列表。我的用户想要点击令牌而不是输入它们。可能有500个令牌,所以listview似乎不可行。它们可以是从一个字符到20个不同的长度,因此gridview似乎不是一个好主意。我能想到的只是将列表保留在textview中,让用户点击一个并弹出一个警告,询问他是否要输入该令牌。该列表是可滚动的。

基本上,我希望用户点击textview中的单词并让java知道那是什么单词。所有单词都以逗号分隔。

例如,这里有一个可能显示的集合

<7> 4W,4F,2R,5K,73,3J,6F,2F,7M,21,5D,1H,5C,24,7Y,4D,70,1E,3P,2C,4B,3E,5A,4G ,5E,6H,6N,7J,7S,2B,41,4H,3H,2A,3B,3F,40,4N,2J,3C,22,5M,44,7Z,3U,3L,3Q,7A,72 ,4V,7X,26,1D,4M,6D,5R,6B,6K,7U,7V,7T,7P,7L,4R,7G,7E,7B,3X,7F,5J,5L,3T,7R,3K ,2T,43,41,5N,1C,7H,5B,3M,6J,6M,6R,5H,1F,1L,7Q,6P,7C,6A,4T,3R,46,1N,1K,4Y,5F ,7K,6S,5Q,2H,42,4X,1A,1B,33,3N,2K,3G,4A,5G,30,4E,7N,4S,3S,5S,3D,20,4K,1J,32 ,2X,31,4Q,45,4Z,1G,74,6G,4C,4U,3Z,4W,5P,25,1M,6C,3V,3Y,3A,6E,4P,7D,4J,4L,3W ,6L,6Q,Thorn Knoll,North Thorn,Hook,Black Jack,Castle Point,Calshot Spit,Bourne Gap,

用户需要能够在30秒内选择所需的10个项目(他在比赛开始前几分钟就获得了该列表)。

他需要输入的文字条目可能如下所示:

路线名称,30,24,4R,3E,6Q,3N,Thorn Knoll,5G

这样做的最佳方式是什么?

这是我现在拥有的 enter image description here

他想要的只是点击令牌。 enter image description here

这里有一些文本代码来证明这个概念。感谢Vaghela M.R - Mobile Devloper的链接。

public class MainActivity extends Activity {
String waypoint = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();
}
private void init() {
    String definition = "1A, 1B, 1C, 1D, 1E, 1F, 1G, 1H, 1J, 1K, 1L, 1M, 1N, 20, 21, 22, 24, 25, 26, 2A, 2B, 2C, 2F, 2H, 2J, 2K, 2R, 2T, 2X, 30, 31, 32, 33, 3A, 3B, 3C, 3D, 3E, 3F, 3G, 3H, 3J, 3K, 3L, 3M, 3N, 3P, 3Q, 3R, 3S, 3T, 3U, 3V, 3W, 3X, 3Y, 3Z, 40, 41, 42, 43, 44, 45, 46, 4A, 4B, 4C, 4D, 4E, 4F, 4G, 4H, 4J, 4K, 4L, 4M, 4N, 4P, 4Q, 4R, 4S, 4T, 4U, 4V, 4W, 4X, 4Y, 4Z, 5A, 5B, 5C, 5D, 5E, 5F, 5G, 5H, 5J, 5K, 5L, 5M, 5N, 5P, 5Q, 5R, 5S, 6A, 6B, 6C, 6D, 6E, 6F, 6G, 6H, 6J, 6K, 6L, 6M, 6N, 6P, 6Q, 6R, 6S, 70, 71, 72, 73, 74, 7A, 7B, 7C, 7D, 7E, 7F, 7G, 7H, 7J, 7K, 7L, 7M, 7N, 7P, 7Q, 7R, 7S, 7T, 7U, 7V, 7W, 7X, 7Y, 7Z, Black_Jack, Bourne_Gap, Calshot_Spit, Castle_Point, Hook, North_Thorn, Thorn_Knoll".trim();
    TextView definitionView = (TextView) findViewById(R.id.text);
    definitionView.setMovementMethod(LinkMovementMethod.getInstance());
    definitionView.setText(definition, BufferType.SPANNABLE);
    Spannable spans = (Spannable) definitionView.getText();
    BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
    iterator.setText(definition);
    int start = iterator.first();
    for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
            .next()) {
        String possibleWord = definition.substring(start, end);
        if (Character.isLetterOrDigit(possibleWord.charAt(0))) 
        {
            ClickableSpan clickSpan = getClickableSpan(possibleWord);
            spans.setSpan(clickSpan, start, end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
private ClickableSpan getClickableSpan(final String word) {
    return new ClickableSpan() {
        final String mWord;
        {
            mWord = word;
        }
        @Override
        public void onClick(View widget) {
            TextView textView1 = (TextView) findViewById(R.id.textView1);
            Log.d("tapped on:", mWord);
            Toast.makeText(widget.getContext(), mWord, Toast.LENGTH_SHORT)
                    .show();
            checkWord(mWord);
        }
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
        }
    };
}
public void doSomething(){
    TextView textView1 = (TextView) findViewById(R.id.textView1);
    StringBuilder sb = new StringBuilder();
    sb.append(textView1.getText());
    sb.append(", ").append(waypoint);
    textView1.setText(sb);
}
public void checkWord(String word) {
    waypoint = word;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Enter "+ word + "?")
           .setCancelable(false)
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    // fire an intent go to your next activity
                   doSomething();
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
}
}

1 个答案:

答案 0 :(得分:0)

感谢@ VaghelaM.R-MobileDevloper的链接,这是一个很好的答案,现在在我的应用程序中工作。有人在之前回答的关于它的标题是浪费时间。 @ VaghelaM.R-MobileDevloper的链接有答案,我编辑了问题以反映我的所作所为。他实际上没有发布答案,所以我不能正式给予他信任。只需阅读扩展的问题并找到工作代码。