如何查找在Textview Android上单击的单词 [英] How to find which word is clicked on a textview Android

查看:82
本文介绍了如何查找在Textview Android上单击的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个令牌列表,用户需要快速创建一个令牌子集的列表.我现在有键盘输入和列表显示,但是键盘掩盖了列表.我的用户想要单击标记而不是键入标记.可能有500个标记,所以列表视图似乎不可行.它们的长度可以从一个字符到20个不同的长度,因此gridview似乎不是一个好主意.我所能想到的就是将列表保留在文本视图中,并让用户单击一个,然后弹出一个警报,询问他是否要输入该令牌.该列表是可滚动的.

I have a list of tokens and the user needs to create a list of a subset of these quickly. I now have keyboard entry and a display of the list but the keyboard covers up the list. My user wants to click on the tokens rather than type them in. There might be 500 tokens so a listview seems unworkable. They can be different lengths from one character to 20 so a gridview doesn't seem like a good idea. All I can think of is to keep the list in a textview and have the user click on one and pop up a alert asking if he wants to enter that token. The list is scrollable.

基本上,我希望用户单击textview中的一个单词并使Java知道是哪个单词.所有单词都用逗号分隔.

Basically, I want the user to click on a word in a textview and have the java know what word that was. All words are separated by commas.

例如,这是可能显示的一组

For example, here is one set that might be displayed

7W,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、71、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,

7W, 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, 71, 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个项目(比赛开始前几分钟,他就会获得列表).

The user needs to be able to select the desired 10 items in under 30 seconds (he gets the list just a few minutes before the start of a race).

他需要输入的文本输入可能看起来像这样:

The text entry that he needs to enter might look like this:

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

Route Name, 30,24,4R, 3E, 6Q, 3N, Thorn Knoll, 5G

做到这一点的最佳方法是什么?

What is the best way to do this?

这就是我现在拥有的

Here is what I have now

他想要的只是单击令牌.

What he wants is to just click on the tokens.

以下是一些文本代码来证明这一概念.感谢Vaghela M.R-Mobile Devloper提供的链接.

Here is some text code to prove the concept. Thanks to Vaghela M.R - Mobile Devloper for the link.

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

推荐答案

感谢@ VaghelaM.R-MobileDevloper提供的链接,这是一个不错的答案,现在可以在我的应用程序中使用.以前有人回答过这个问题的标题已经很浪费时间了. @ VaghelaM.R-MobileDevloper的链接有答案,我编辑了问题以反映我的所作所为.他实际上没有发布答案,所以我不能正式相信他.只需阅读扩展的问题并找到有效的代码即可.

Thanks to @VaghelaM.R-MobileDevloper for the link this is a nice answer that is now working in my app. The header someone put on this about it having been previously answered is a waste of time. @VaghelaM.R-MobileDevloper's link had the answer and I edited the question to reflect what I did. He did not actually post an answer so I cannot credit him formally. Just read the expanded question and find the working code.

这篇关于如何查找在Textview Android上单击的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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