我在mainactivity中有一系列按钮,当我单击其中的一个按钮时,我想显示其ID,但是使用view.getId()我得到一个整数并将其转换为字符串,我得到一串数字,怎么办我得到我写在xml上的ID?
public void giocaCarta(View view){
Toast.makeText(this,Integer.toString(view.getId()),Toast.LENGTH_LONG).show(); }
这是按钮的代码:
<Button
android:id="@+id/btn_g2_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="giocaCarta"
android:text="Button" />
我想读烤面包片“ btn_g2_1”而不是数字序列,有什么方法可以提供这个功能?
答案 0 :(得分:1)
您可以使用此:
String viewIdName = getResources().getResourceEntryName(myView.getId())
答案 1 :(得分:1)
我想知道为什么您需要它,但要获取该ID,请使用此代码
view.getResources().getResourceName(view.getId());