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

时间:2022-11-21 20:48:41

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?

现在这很好用。它显示生成的查询。现在,我想创建另一个活动,在显示查询时响应长按或只触摸一次。如何获取按下的textview的ID,以便我可以将其用于其他活动?

many thanks for your help.

非常感谢您的帮助。

2 个解决方案

#1


8  

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”。

#2


1  

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

    }

#1


8  

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”。

#2


1  

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

    }