【问题标题】:How to click or tap on a TextView text如何单击或点击 TextView 文本
【发布时间】:2011-03-20 17:11:09
【问题描述】:

我知道这很容易(doh...),但我正在寻找一种方法来运行在 Android 应用中点击或单击 TextView 文本行的方法。

我一直在考虑按钮侦听器和匿名方法侦听器调用,但它似乎不适用于 TextView。

谁能指点我一些代码 sn-p 以显示在 TextView 中单击或点击一段文本如何运行方法?

【问题讨论】:

  • 你真的应该接受最重要的答案。
  • 他必须登录才能这样做。
  • 而且他还拿走了一个好听的用户名:/
  • 未来,如果有人在使用 Kotlin 并希望通过回调完全控制可点击文本 - 我写了一篇关于它的文章,为TextView - link.medium.com/TLq6s8ltc3 提供扩展功能跨度>

标签: android onclick listener textview


【解决方案1】:

您可以使用以下属性在 xml 中设置点击处理程序:

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

不要忘记 clickable 属性,没有它,点击处理程序不会被调用。

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) {
            ...
          }  
       }

【讨论】:

  • "不要忘记 clickable 属性,没有它,点击处理程序不会被调用。"这条线节省了 y 天。非常感谢朋友。
  • developer.android.com/reference/android/view/… 需要提及一些关于可点击的内容。本来可以节省一个小时的。
  • 有趣的是,使用setOnClickListener 设置clickable 属性,但使用onClick 属性显然没有。
  • 似乎onClick 确实 在 Android 5.0 Lollipop (API 21) 中设置了 clickable 属性。也许他们认为这是旧版本中没有发生的错误?
  • 有没有办法通过 XML 实现长点击?
【解决方案2】:

这可能不是您想要的,但这对我正在做的事情有用。这一切都是在我的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");
    }
});

【讨论】:

    【解决方案3】:

    好的,我已经回答了我自己的问题(但这是最好的方法吗?)

    这是当您单击或点击 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");  
        }
    }
    

    而我的 main.xml 是:

    <?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>
    

    【讨论】:

      【解决方案4】:

      在调用布局和文本视图的 Activity 中,此点击监听器起作用:

      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());
                      }
                  }
              });
      

      文本视图声明如下(默认向导):

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

      在strings.xml文件中

      <string name="menu_id_google">Google ID (Gmail)</string>
      

      【讨论】:

        【解决方案5】:

        虽然可以通过将监听器设置为 textview 来解决问题,但建议不要这样做。您应该使用flat button,因为它是 Button 的子类,它提供了许多 TextView 没有的属性。


        要使用平面按钮,请添加style="?android:attr/borderlessButtonStyle" 属性-

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

        【讨论】:

        • 您可能还想添加 android:textAllCaps="false" 以允许使用小写文本。
        【解决方案6】:

        在文本视图中

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

        您还必须实现 View.OnClickListener 并且在 On Click 方法中可以使用意图

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

        我测试过这个解决方案可以正常工作。

        【讨论】:

          【解决方案7】:

          要点击一段文本(不是整个TextView),您可以使用HtmlLinkify(不过,两者都创建打开网址的链接,而不是应用程序中的回调)。

          Linkify

          使用字符串资源,例如:

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

          然后在文本视图中:

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

          Html

          使用Html.fromHtml

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

          然后在你的文本视图中:

          textView.setText(Html.fromHtml(getString(R.string.html)));
          

          【讨论】:

            【解决方案8】:

            您可以将 TextWatcher 用于 TextView,比 ClickLinstener 更灵活(不是最好或更坏,只有一种方式)。

            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!
            
                    }
                });
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-09-30
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-02-14
              • 1970-01-01
              相关资源
              最近更新 更多