2011-04-04 74 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

相關問題