【问题标题】:Selecting Text from a TextView or EditText从 TextView 或 EditText 中选择文本
【发布时间】:2013-11-12 08:50:42
【问题描述】:

我有几个TextView 和几个EditText,我想从中选择一个数据的子字符串来复制API 级别10 及更高级别。我已经为这个问题实现了一个OnLongClickListener,但是它(可以理解) 抛出 ArrayOutOfBoundsException:

   public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    //copy
    longPressedView=v;
    startSelection=((TextView)v).getSelectionStart();
    endSelection=((TextView)v).getSelectionEnd();
    Log.d(TAG, "Selection starts at "+startSelection+" and ends at "+endSelection);
    if(startSelection>endSelection)
    {
        startSelection=startSelection+endSelection;
        endSelection=startSelection-endSelection;
        startSelection=startSelection-endSelection;
        Log.d(TAG, "After interchanging positions selection starts at "+startSelection+" and ends at "+endSelection);
    }
    mSelectedText=((TextView)v).getText().toString().substring(startSelection, endSelection);
    mActionMode=startActionMode(actionModeCallback);
    return true;
}

我考虑过实现 OnTouchListener,但这只会返回 x 和 y 位置,这对我没有用处。

 java.lang.StringIndexOutOfBoundsException: length=93; regionStart=-1; regionLength=0
 at java.lang.String.startEndAndLength(String.java:583)
 at java.lang.String.substring(String.java:1464)
 at com.example.clipboardtest.MainActivity.onClick(MainActivity.java:139)
 at android.view.View.performClick(View.java:4240)
 at android.view.View$PerformClick.run(View.java:17721)
 at android.os.Handler.handleCallback(Handler.java:730)
 at android.os.Handler.dispatchMessage(Handler.java:92)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:5103)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:525)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 在您的日志中打印的startselectionendselection 值是什么?还有mSelectedText的值是多少?
  • 什么都没有,而是抛出了一个很大的StringIndexOutOfBoundsException,顺便说一下我用TextView试试。
  • 首先,当你第一次长按文本视图时。开始选择和结束选择是-1。这就是为什么,那个错误来了
  • 它没有从 TextView 中选择文本。

标签: android textview android-edittext clipboardmanager


【解决方案1】:

首先在你的 xml 中的 textview 中添加这个属性: -

android:textIsSelectable="true"

然后使用此代码:-

    t = (TextView)findViewById(R.id.textView1);
    t.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            int start = t.getSelectionStart();
            int end = t.getSelectionEnd();
            if(start == -1 && end == -1){
                return true;
            }
            String mSelectedText=((TextView)v).getText().toString().substring(start, end);
            System.out.println(mSelectedText);

            return false;
        }
    });

【讨论】:

  • 我发现android:textIsSelectable 仅适用于 API 级别 11 及更高级别。所以,我已经开始使用 EditTextandroid:editable=false。现在,我无法选择文本,因为光标是即使我选择使用android:cursorVisible=true 也不可见
  • 所以将 api 级别更改为 11。有什么大不了的
  • 无论发生什么,都应在 API 级别 10 上测试应用程序......所以不,我无法将其更改为 API 级别 11。如果我这样做,那将是一件大事。
  • 此功能从 11 开始可用。查看此功能:- stackoverflow.com/questions/6625300/…
【解决方案2】:

OnClickListener 怎么样?

text_field.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                //-------- Your code goes here
            }
        });

【讨论】:

  • EditText 不能使用它。我已经用错误日志更新了我的帖子。检查出来。
猜你喜欢
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
  • 2011-12-11
  • 2011-08-07
相关资源
最近更新 更多