Android button versus TextView - hyperlink behavior

In the Android view, I want to have interactive text that looks like a hyperlink to a web page.

It looks the same as regular text (without a button frame), but when I touch, I want to change the color of the text, and also want its background to rotate the inverse color.

Is it better to use a button with a transparent background or TextView. When should I choose one by one?

Thank you so much

+5
source share
5 answers

hotveryspicy gave a great answer and really pointed me in the right direction. Thanks, but I had a few questions with the answer.

What I want is a button with black text and a gray background in the normal state and a red background with white text in the selected state. Like this:

enter image description here

hotveryspicy, :

  • drawable textColor. , . , XML ?

  • , : buttonselector.xml. , , drawable.

, , :

Color State Resource, :

: res/colors/link_button.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="#ffffff"/> 
    <item android:color="#000000"/> 
</selector>

3 :

Shape Drawable: : res/drawable/link_button.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <solid  android:color="#cccccc"/>
</shape>

, Shape Drawable: : res/drawable/link_button_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">
    <solid  android:color="#ff0000"/>
</shape>

Drawable Selector, . : res/drawable/link_button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

 <item 
     android:drawable="@drawable/link_button_selected"
     android:state_pressed="true" />

 <item 
     android:drawable="@drawable/link_button" />
</selector>

:

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:text="Button"
    android:textColor="@color/link_button" 
    android:background="@drawable/link_button_selector"
    />

!

+2

Button selector .

textselector.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="#0000ff"/> <!-- pressed -->
    <item android:state_focused="true" android:color="@android:color/white"/> <!-- focused -->
    <item android:color="@android:color/white"/> <!-- default -->

</selector>

buttonselector.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="@android:color/white"/> <!-- pressed -->
    <item android:state_focused="true" android:color="#0000ff"/> <!-- focused -->
    <item android:color="#0000ff"/> <!-- default -->

</selector>

xml

<Button
     android:id="@+id/button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/buttonselector"
     android:text="some text"
     android:textColor="@drawable/textselector" />
+3

  <string name="link">'<a href="http://www.google.com" rel="nofollow">Google</a>'</string>  

<TextView  
          android:id="@+id/link"  
          android:layout_width="wrap_content"  
          android:layout_height="wrap_content"  
          android:autoLink="all"  
          android:linksClickable="true"  
          android:text="@string/link" />  
+1

linkify. this

+1

:

, , TextView ? , . , , .

TextView.setOnClickListener(new View.OnClickListener()
+1
source

All Articles