简体   繁体   中英

Android select and highlight text in edittext

I would like to do an app which has an EditText or TextView that can be selected upon click and highlight the selected text. How can I do that? I tried overriding onClick method on my EditText but seems not working.

Here's what I've tried so far:

etx.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            int startSelection = etx.getSelectionStart();
            int endSelection = etx.getSelectionEnd();

            //String selectedText = etx.getText().toString().substring(startSelection, endSelection);

            Spannable spannable=new SpannableString(etx.getText().toString());
            spannable.setSpan(new ForegroundColorSpan(Color.BLUE), startSelection, endSelection, 0);
            etx.setText(spannable); 

            return true;
        }

    });



 <EditText 
        android:id="@+id/tvOrdinanceTitle" 
        android:layout_width="wrap_content" 
        android:textColor="@android:color/black"
        android:cursorVisible="false"
        android:layout_height="wrap_content"
        android:background="#00000000" > 
        </EditText>

But it's not working. Any workaround? I would gladly appreciate your help. Thanks.

You can include in your .xml:

android:selectAllOnFocus="true"

Specifically:

android:textIsSelectable=Indicates that the content of a non-editable text can be selected. 
android:selectAllOnFocus=If the text is selectable, select it all when the view takes. focus. 

Or programmatically:

etx.setOnFocusChangeListener(new OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
    if(hasFocus)
    { 
        etx.setSelection(editText.getText().toString().length());
    }
}
});

And to identify what colors to be used. Lets say this is your editText:

    <EditText 
    android:id="@+id/tvOrdinanceTitle" 
    android:layout_width="wrap_content" 
    android:cursorVisible="false"
    android:layout_height="wrap_content"
    android:background="@color/etxt_color" > 
    </EditText>

Then create res/color/etxt_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
       android:color="#000000" /> <!-- pressed -->
 <item android:state_focused="true"
       android:color="#000000" /> <!-- focused -->
 <item android:color="#FFFFFF" /> <!-- default -->
 </selector>

you can better use a TextView with a background set like and EditText ,

since using on click listener on EditText is not a good practice because it shows the keyboard when you click on it, but still if you want to use EditText for the same purpose , you can refer this: Android EditText onClickListener

editText.selectAll()是对我editText.selectAll()的函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM