【问题标题】:Get ID of edittext view programmatically? [duplicate]以编程方式获取edittext视图的ID? [复制]
【发布时间】:2018-12-06 09:10:54
【问题描述】:

这是我的 Edittext 的 XML 代码,我用这个名称设置了 id ettMId

                <EditText
                android:id="@+id/ettMId"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_hint_email_id"
                android:imeOptions="actionNext"
                android:inputType="text|textEmailAddress"
                android:nextFocusDown="@id/etLPassword"
                android:singleLine="true"
                android:theme="@style/AppEditTextStyle" />

我希望在我的活动类中以编程方式使用该 ID。

我试过这段代码来获取 id

   int id=ettMId.getId();

但我得到了以下结果

 If2131230797

实际上我想要我在 XML 中设置的相同 id 名称(ettMId

【问题讨论】:

  • 为什么不能检查它是否等于R.id.ettMId
  • 你为什么要这样做?
  • @Michael 是的,我在同等条件下这样做,但我有超过 1k 的 Edittext 字段。
  • 好的,但是即使您将 id 作为字符串获取,您仍然有 1000 个 EditText 字段。我看不出有一个字符串会有什么帮助。
  • @Arbaz.in 在每个 Editext 集合标签中也像 id 一样,然后在你的活动中获取标签,正如我告诉你的 et.getTag() 它会返回你的字符串标签

标签: android android-studio android-edittext android-xml


【解决方案1】:

试试这个

String stringID = ettMId.getResources().getResourceName(ettMId.getId());

然后你可以进行字符串操作来提取:id/之后的id的确切值

【讨论】:

  • 这是否适用于 minifyEnabled?
  • 感谢它的工作...
  • @ZUNJAE 是的,它与 minifyEnabled true 一起工作
猜你喜欢
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 1970-01-01
  • 2016-10-27
  • 2016-11-10
  • 2011-06-10
相关资源
最近更新 更多