当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《如何从TextView中显示每个单击的单词》经验,为你挑选了1个好方法。

我计划开发一个具有非常简单概念的应用程序。要求是我想在意图选择器的帮助下从电话中添加一个文本文件(动态,因此无法为跨度字符串设置clickabble位置的跨度)。并且需要在textView中显示所选文件的内容(如果有建议,则显示任何视图)。一旦我单击了textview内容中的任何单词,我就需要在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]



1> ASKAR ALI..:

最后我得到了我的问题的确切答案

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

推荐阅读
手机用户2402852307
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有