【问题标题】:Android - How to know the long-pressed text in the TextView?Android - 如何知道 TextView 中的长按文本?
【发布时间】:2013-04-19 18:50:37
【问题描述】:

正如标题所说,我想知道如何知道TextView中长按的文本。

caption.setOnLongClickListener(new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        Log.d("log", "caption clicked");
        String text = ((TextView)v).getText().toString();
        Log.d("log", "1: " + text);
        int start = ((TextView)v).getSelectionStart();
        Log.d("log", "2: " + start);
        int end = ((TextView)v).getSelectionEnd();
        Log.d("log", "3: " + end);
        Log.d("log", "selection: " + text.substring(start, end));


        return true;
    }
});

在这里,开始和结束变量都得到 -1。它不应该返回我长按的文本索引吗? 有没有其他方法可以知道textView中长按的文字?

【问题讨论】:

    标签: android textview long-press


    【解决方案1】:

    您可以继承EditText 并覆盖其onSelectionChanged() 方法。

    【讨论】:

      猜你喜欢
      • 2014-01-26
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多