How to click or tap on a TextView text

后端 未结 8 1642
無奈伤痛
無奈伤痛 2020-11-27 09:52

I know this is so easy (doh...) but I am looking for a way to run a method on tapping or clicking a TextView line of text in an Android App.

I keep thinking about bu

相关标签:
8条回答
  • 2020-11-27 10:23

    in textView

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:onClick="onClick"
        android:clickable="true"
    

    You must also implement View.OnClickListener and in On Click method can use intent

        Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
               intent.setData(Uri.parse("https://youraddress.com"));    
                startActivity(intent);
    

    I tested this solution works fine.

    0 讨论(0)
  • 2020-11-27 10:30

    You can set the click handler in xml with these attribute:

    android:onClick="onClick"
    android:clickable="true"
    

    Don't forget the clickable attribute, without it, the click handler isn't called.

    main.xml

        ...
    
        <TextView 
           android:id="@+id/click"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"               
           android:text="Click Me"
           android:textSize="55sp"
           android:onClick="onClick"                
           android:clickable="true"/>
        ...
    

    MyActivity.java

           public class MyActivity extends Activity {
    
              public void onClick(View v) {
                ...
              }  
           }
    
    0 讨论(0)
提交回复
热议问题