在Android中使TextView可点击

6

我是在制作一个Android应用程序,我有四个TextView分别为ProductId,Title,Description和Image。我希望当我点击它们中的每一个时能够显示产品ID。我有一个用于此目的的Web服务。

Web服务的输出如下:

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>

感谢您的提前帮助,Tushar


这里的问题是什么?你到目前为止尝试了什么? - Octavian Helm
我已经创建了一个包含4个文本的XML文件..这是我的Java代码。 - Tushar
如果我理解正确的话,您想要为每个TextView定义一个ClickListener,以便它可以例如显示带有产品ID的弹出消息。 - gnclmorais
4个回答

18
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
  }

});

7
您也可以使用 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中实现“长点击”? - Joshua Pinter
没有内置的方法,但是你仍然可以自定义你的 View 来实现这一功能。https://dev59.com/qG025IYBdhLWcg3w76pq#13417824 - Zar E Ahmer
好的,谢谢确认。最终在Java Activity文件中使用了setOnLongClickListener(new View.OnLongClickListener() {...} - Joshua Pinter

1
您可以像下面这样简单地为您的文本视图创建OnClickListeners
TextView textview = new TextView(this);
        textview.setText("This is a textview");

        textview.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // do something here.
            }
        });

0
这个点击监听器有效:
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" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接