簡體   English   中英

在android中使textview可點擊

[英]making a textview clickable in android

我正在制作一個Android應用程序,我有4個文本視圖,即ProductId,標題,描述,圖像。我希望當我點擊它們中的每一個時,應該顯示產品ID。我有一個web服務。

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>

在此先感謝Tushar

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
  }

});

你也可以使用像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;
   }
}

您可以簡單地創建OnClickListeners ,如:

TextView textview = new TextView(this);
        textview.setText("This is a textview");

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

這個點擊監聽器有效:

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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM