2011-04-04 76 views
5

我正在做一个Android应用程序,我有4个textviews,即ProductId,标题,说明,image.I想当我点击他们每个人产品ID应该显示。我有一个web服务这个。 Web服务的制作一个textview可点击在android

输出为预先

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> 

感谢图莎尔

+0

这里有什么问题?你试过什么了? – 2011-04-04 10:12:10

+0

我已经创建了一个带有4个文本的xml。这里是我的java代码 – User 2011-04-04 10:13:28

+0

如果我正确理解它,您希望为每个TextView定义一个ClickListener,因此它可以显示带有产品ID的多士炉。 – gnclmorais 2011-04-04 10:17:04

回答

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

}); 
0

您可以简单地创建textviews的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" /> 
3

,你也可以做使用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 like

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

你能用XML来做一个“长按”吗? – 2017-11-02 15:59:16

+0

没有任何构建方法。但是你仍然可以自定义视图来执行此操作。 https://stackoverflow.com/a/13417824/3496570 – Nepster 2017-11-03 05:51:10

+0

好的,谢谢你的确认。在Java活动文件中使用'setOnLongClickListener(new View.OnLongClickListener(){...}'结束。 – 2017-11-05 15:37:07

相关问题