How to click or tap on a TextView text

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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     >
    <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content"             android:layout_height="wrap_content"></LinearLayout>
    <ListView android:id="@+id/ListView01" android:layout_width="wrap_content"   android:layout_height="wrap_content"></ListView>
    <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content"   android:layout_height="wrap_content"></LinearLayout>
    
    <TextView android:text="This is my first text"
     android:id="@+id/TextView01" 
     android:layout_width="wrap_content" 
     android:textStyle="bold"
     android:textSize="28dip"
     android:editable = "true"
     android:clickable="true"
     android:layout_height="wrap_content">
     </TextView>
     </LinearLayout>
    
    0 讨论(0)
  • 2020-11-27 10:10

    To click on a piece of the text (not the whole TextView), you can use Html or Linkify (both create links that open urls, though, not a callback in the app).

    Linkify

    Use a string resource like:

    <string name="links">Here is a link: http://www.stackoverflow.com</string>
    

    Then in a textview:

    TextView textView = ...
    textView.setText(R.string.links);
    Linkify.addLinks(textView, Linkify.ALL);
    

    Html

    Using Html.fromHtml:

    <string name="html">Here you can put html &lt;a href="http://www.stackoverflow.com"&gt;Link!&lt;/&gt;</string>
    

    Then in your textview:

    textView.setText(Html.fromHtml(getString(R.string.html)));
    
    0 讨论(0)
  • 2020-11-27 10:14

    Although you can resolve the problem by setting the listener to textview, it's recommended not to. You should use flat button as it is a subclass of Button and it provides many attributes which TextView doesn't.


    To use flat button, add style="?android:attr/borderlessButtonStyle" attribute -

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DONE"
        style="?android:attr/borderlessButtonStyle"/>
    
    0 讨论(0)
  • 2020-11-27 10:16

    You can use TextWatcher for TextView, is more flexible than ClickLinstener (not best or worse, only more one way).

    holder.bt_foo_ex.addTextChangedListener(new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // code during!
    
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // code before!
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                // code after!
    
            }
        });
    
    0 讨论(0)
  • 2020-11-27 10:21

    This may not be quite what you are looking for but this is what worked for what I'm doing. All of this is after my onCreate:

    boilingpointK = (TextView) findViewById(R.id.boilingpointK);
    
    boilingpointK.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if ("Boiling Point K".equals(boilingpointK.getText().toString()))
                boilingpointK.setText("2792");
            else if ("2792".equals(boilingpointK.getText().toString()))
                boilingpointK.setText("Boiling Point K");
        }
    });
    
    0 讨论(0)
  • 2020-11-27 10:23

    from inside an activity that calls a layout and a textview, this click listener works:

    setContentView(R.layout.your_layout);
    TextView tvGmail = (TextView) findViewById(R.id.tvGmail);
    String TAG = "yourLogCatTag";
    tvGmail.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View viewIn) {
                    try {
                        Log.d(TAG,"GMAIL account selected");
                    } catch (Exception except) {
                        Log.e(TAG,"Ooops GMAIL account selection problem "+except.getMessage());
                    }
                }
            });
    

    the text view is declared like this (default wizard):

            <TextView
                android:id="@+id/tvGmail"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="@string/menu_id_google"
                android:textSize="30sp" />
    

    and in the strings.xml file

    <string name="menu_id_google">Google ID (Gmail)</string>
    
    0 讨论(0)
提交回复
热议问题