How to click or tap on a TextView text

后端 未结 8 1678
無奈伤痛
無奈伤痛 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:09

    OK I have answered my own question (but is it the best way?)

    This is how to run a method when you click or tap on some text in a TextView:

    package com.textviewy;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.TextView;
    
    public class TextyView extends Activity implements OnClickListener {
    
    TextView t ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        t = (TextView)findViewById(R.id.TextView01);
        t.setOnClickListener(this);
    }
    
    public void onClick(View arg0) {
        t.setText("My text on click");  
        }
    }
    

    and my main.xml is:

    
    
    
    
    
    
    
     
     
    

提交回复
热议问题