如何从 TextView 显示每个点击的单词

编程入门 行业动态 更新时间:2024-10-15 20:20:52
本文介绍了如何从 TextView 显示每个点击的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我计划开发一个概念非常简单的应用程序.要求是我想从手机中添加一个文本文件(动态,所以设置跨度与可点击位置不可用于跨度字符串)与意图选择器的帮助.并且需要在 textView 中显示选定的文件内容(如果您建议,则为任何视图).单击 textview 内容中的任何单词后,我需要在 Toast 中显示该单词.

I am planned to develop an App with very simple concept.The requirement is I want to add a text file from phone(Dynamic, so set span with clickabble position not possible for spannable string) with help of intent chooser. And need to display the selected file content in textView (any view if u suggested). Once I clicked any of the word from textview content i need display that word in Toast.

    button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFileChooser();
            }
        });



 private void showFileChooser() {

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        //intent.setType("*/*");      //all files
        intent.setType("text/xml");   //XML file only
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        try {
            startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), 1);
        } catch (android.content.ActivityNotFoundException ex) {
            // Potentially direct the user to the Market with a Dialog
            Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
        }
    }


 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                // User pick the file
                Uri uri = data.getData();
                ExternalFileHandling fileObj=new ExternalFileHandling(getApplicationContext());
                String fileContent = fileObj.readTextFile(uri);
                aboutTextView.setText(fileContent);
                Toast.makeText(this, fileContent, Toast.LENGTH_LONG).show();
            } else {
                Log.i("data", data.toString());
            }
        }``


public String readTextFile(Uri uri){
        BufferedReader reader = null;
        StringBuilder builder = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(Currentcontext.getContentResolver().openInputStream(uri)));
            String line = "";

            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return builder.toString();
    }

[![intent chooser for dynamic file content][1]][1]

推荐答案

我的问题终于得到了确切的答案

Finally I got the Exact answer for my question

aboutTextView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                mOffset = aboutTextView.getOffsetForPosition(motionEvent.getX(), motionEvent.getY());
              //  mTxtOffset.setText("" + mOffset);
                Toast.makeText(HomeScreen.this, findWordForRightHanded(aboutTextView.getText().toString(), mOffset), Toast.LENGTH_SHORT).show();

            }
            return false;
        }
    });



private String findWordForRightHanded(String str, int offset) { // when you touch ' ', this method returns left word.
    if (str.length() == offset) {
        offset--; // without this code, you will get exception when touching end of the text
    }

    if (str.charAt(offset) == ' ') {
        offset--;
    }
    int startIndex = offset;
    int endIndex = offset;

    try {
        while (str.charAt(startIndex) != ' ' && str.charAt(startIndex) != '\n') {
            startIndex--;
        }
    } catch (StringIndexOutOfBoundsException e) {
        startIndex = 0;
    }

    try {
        while (str.charAt(endIndex) != ' ' && str.charAt(endIndex) != '\n') {
            endIndex++;
        }
    } catch (StringIndexOutOfBoundsException e) {
        endIndex = str.length();
    }

    // without this code, you will get 'here!' instead of 'here'
    // if you use only english, just check whether this is alphabet,
    // but 'I' use korean, so i use below algorithm to get clean word.
    char last = str.charAt(endIndex - 1);
    if (last == ',' || last == '.' ||
            last == '!' || last == '?' ||
            last == ':' || last == ';') {
        endIndex--;
    }

    return str.substring(startIndex, endIndex);
}

这篇关于如何从 TextView 显示每个点击的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 19:22:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1398890.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   TextView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!