简体   繁体   English

android:单击时如何获取textview的ID?

[英]android: How to get the ID of a textview when clicked?

I have an activity that queries from sqllite and display the output to xml 我有一个活动,可以从sqllite查询并将输出显示为xml

this is part of the activity 这是活动的一部分

( (TextView) view.findViewById(R.id.tv_id) ).setText( listItem.getId()+"");
( (TextView) view.findViewById(R.id.tv_name) ).setText( listItem.getName() );
( (TextView) view.findViewById(R.id.tv_age) ).setText( listItem.getAge()+"" );

and this is the xml: 这是xml:

<TableLayout 
  android:layout_width="wrap_content" 
  android:id="@+id/TableLayout01" 
  android:layout_below="@+id/tv_age" 
  android:layout_height="wrap_content" 
  android:layout_toRightOf="@+id/tv_name">
  <TableRow 
   android:layout_width="wrap_content" 
   android:layout_below="@+id/tv_age" 
   android:layout_height="wrap_content" 
   android:layout_toRightOf="@+id/TableLayout01"   
   android:id="@+id/TableRow01">
   <TextView 
    android:layout_width="10dip" 
    android:layout_height="30dip"     
    android:id="@+id/tv_id" 
    android:text=""     
    android:layout_centerVertical="true">
   </TextView>
   <TextView 
    android:layout_width="100dip" 
    android:layout_height="wrap_content" 
    android:text="" android:id="@+id/tv_name" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@+id/tv_id" 
    android:layout_marginLeft="10px">
   </TextView>
   <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="" android:id="@+id/tv_age" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@+id/tv_name" 
    android:layout_marginLeft="10px">
   </TextView>
  </TableRow>
 </TableLayout>

Now this works fine. 现在可以正常工作了。 It displays the resulting query. 它显示结果查询。 Now, I want to create another activity which responds to the long press or just one touch when the query is displayed. 现在,我想创建另一个活动,该活动可以响应长按或显示查询时只需轻按一下。 How can I get the ID of the pressed textview so I can use it for the other activity? 如何获取按下的文本视图的ID,以便将其用于其他活动?

many thanks for your help. 非常感谢您的帮助。

Do like this instead: 改为这样:

TextView tvId = (TextView) findViewById(R.id.tv_id);
tvId.setText( listItem.getId()+"");
tvId.setOnClickListener(this);

and then in the onClickEvent you get the id of the text view like "apps" write, with the getId() method. 然后在onClickEvent ,使用getId()方法获取文本视图的ID,例如“ apps” write。

if you want the String of the id you can use the following method: 如果您想要ID的字符串,则可以使用以下方法:

String name = getIDName(MyTextView, R.id.class);

public static String getIDName(View view, Class<?> clazz) throws Exception {

        Integer id = view.getId();
        Field[] ids = clazz.getFields();
        for (int i = 0; i < ids.length; i++) {
            Object val = ids[i].get(null);
            if (val != null && val instanceof Integer
                    && ((Integer) val).intValue() == id.intValue()) {
                return ids[i].getName();
            }
        }

        return "";

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM