How to make a part of text clickable like button i

2020-03-01 19:59发布

问题:

I would like to add button myButton to TextView. How to get it in a single line so that it looks like one set of lines. Here is sample of the text to be added

Please press this here to refresh the set of values.

I want to have "here" as clickable button. How can I do it. Any snippet on it would be helpful.

回答1:

make three textView ex: textView1, textView2 and textView3.

<RelativeLayout android:id="@+id/textLay"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    >

    <TextView android:id="@+id/textView1" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Please press this"/>

    <TextView android:id="@+id/textView2" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:text="here" 
        android:layout_toRightOf="@+id/textView1"  />

    <TextView android:id="@+id/textView3" android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
         android:text="to refresh the set of values." 
        android:layout_toRightOf="@+id/textView2"  />

</RelativeLayout>


回答2:

Set background of Button as null by android:background="@null" give the same text color as given to other TextView's.

or You can take 3 TextView's and setOnClickListener to middle one.



回答3:

Set style of Button with borderless android attribut to remove background and border:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="?android:attr/borderlessButtonStyle"
    android:text="here" />


回答4:

Just add below attribute to TextView in XML layout file to make it clickable.

android:clickable="true"

In your Java file add OnClickListener to complete click action on text view.



回答5:

Add android:clickable="true" and android:onClick="yourclickname" for the TextView in the XML. Then define yourclickname in your activity. This should make your TextView clickable and triggers the specified method when clicked.