【问题标题】:making a textview clickable in android在android中制作一个可点击的textview
【发布时间】:2017-11-02 22:32:23
【问题描述】:

我正在制作一个 android 应用程序,我有 4 个文本视图,即 ProductId、Title、description、image。我希望在单击每一个时都应显示产品 ID。我为此提供了一个网络服务。

webservice 的输出是

vmsc>
<response code="0" message="Success"/>
−
<responsedata>
−
<productcategories>
−
<productcategory>
<id>1</id>
<title>Celebrities</title>
<description>Celebrities</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>2</id>
<title>Music</title>
<description>Music</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>3</id>
<title>Sports</title>
<description>Sports</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>4</id>
<title>Fashion</title>
<description>Fashion</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>5</id>
<title>Religion</title>
<description>Religion</description>
<image>
        </image>
</productcategory>
−
<productcategory>
<id>6</id>
<title>Others</title>
<description>Others</description>
<image>
        </image>
</productcategory>
</productcategories>
</responsedata>
</vmsc>

提前致谢 图沙尔

【问题讨论】:

  • 这里的问题是什么?到目前为止,您尝试过什么?
  • 我创建了一个包含 4 个文本的 xml。这是我的 java 代码
  • 如果我理解正确的话,您想为每个 TextView 定义一个 ClickListener,例如,它可以显示一个带有 Product ID 的烤面包机。

标签: android


【解决方案1】:
final TextView view = (TextView) findViewById(R.id.textview1);
view.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View v) {
    // request your webservice here. Possible use of AsyncTask and ProgressDialog
    // show the result here - dialog or Toast
  }

});

【讨论】:

    【解决方案2】:

    你也可以使用xml来做到这一点

          <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="handleOnClick"
            android:clickable="true"
            android:text="Clickable TextView"
            android:textSize="30sp" />
    

    并像处理 onClick 一样

    public void handleOnClick(View view)
    {
       switch(view.getId())
       { 
          case R.id.textView:
          //do your stufff here..
          break;
          default: 
          break;
       }
    }
    

    【讨论】:

    • 您也可以在 XML 中进行“长按”吗?
    • 没有任何内置方法。但是您仍然可以自定义视图来执行此操作。 stackoverflow.com/a/13417824/3496570
    • 好的,谢谢您的确认。最终在 Java Activity 文件中使用了setOnLongClickListener(new View.OnLongClickListener() {...}
    【解决方案3】:

    您可以简单地为您的文本视图创建OnClickListeners,例如:

    TextView textview = new TextView(this);
            textview.setText("This is a textview");
    
            textview.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // do something here.
                }
            });
    

    【讨论】:

      【解决方案4】:

      此点击监听器有效:

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

      声明文本视图时不引用android:clickable="true"(默认向导):

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

      【讨论】:

        猜你喜欢
        • 2016-04-07
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 1970-01-01
        • 2015-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多