Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让文本视图在android中可点击_Android - Fatal编程技术网

让文本视图在android中可点击

让文本视图在android中可点击,android,Android,我正在制作一个android应用程序,我有4个文本视图,分别是ProductId、Title、description、image。我想当我点击其中的每一个时,都应该显示product id。我有一个用于此的Web服务 webservice的输出是 vmsc> <response code="0" message="Success"/> − <responsedata> − <productcategories> − <productcategory

我正在制作一个android应用程序,我有4个文本视图,分别是ProductId、Title、description、image。我想当我点击其中的每一个时,都应该显示product 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>
vmsc>
−
−
−
1.
名人
名人
−
2.
音乐
音乐
−
3.
体育
体育
−
4.
时尚
时尚
−
5.
宗教
宗教
−
6.
其他
其他
提前谢谢
Tushar

您只需创建文本视图的
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”(默认向导):


您也可以使用类似xml的xml来实现


这里的问题是什么?到目前为止,您尝试了什么?我已经创建了一个包含4个文本的xml。这是我的java代码。如果我理解正确,您希望为每个TextView定义一个ClickListener,以便它可以(例如)显示一个带有产品ID的烤面包机。您也可以在xml中进行“长时间单击”吗?没有任何内置方法。但您仍然可以自定义视图来实现这一点。好的,谢谢你的确认。最后在Java活动文件中使用了
setOnLongClickListener(new View.OnLongClickListener(){…}
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());
                }
            }
        });
        <TextView
            android:id="@+id/tvGmail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/menu_id_google"
            android:textSize="30sp" />
      <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" />
public void handleOnClick(View view)
{
   switch(view.getId())
   { 
      case R.id.textView:
      //do your stufff here..
      break;
      default: 
      break;
   }
}