Android select and highlight text in edittext

时间秒杀一切 提交于 2019-12-08 03:39:27

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>
Raviindra

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!