根据构建类型预填充 EditText
Prefill EditText according to buildtype
是否可以在 EditText
中仅针对特定的构建类型设置文本?
我希望在 运行 调试构建类型时预填充我正在开发的应用程序中的 EditText
。
我现在看到的唯一方法是以编程方式检查当前构建类型是否为 "debug" 并调用 setText()
.
我希望能够以更简洁的方式执行此操作。也许类似于 XML 布局中的 tools
命名空间。
有什么建议吗?
您可以在 buildTypes
中的 build.gradel
文件中为不同的环境添加一些文本
//For Development Environment
buildConfigField "String", "text", "\"DEVELOPMENT ENVIRONMENT TEXT\""
//For Live Environment leave it empty
buildConfigField "String", "text", "\"\""
然后在 activity 中直接将其设置为您的编辑文本,无需手动检查任何内容。
etValue.setText(BuildConfig.text);
更优选的解决方案(直接XML)
而不是 buildConfigField
使用 resValue
,它将在项目重建时为不同的环境生成 String Resource
。
//For Live Environment leave it empty
resValue "string", "text", YOUR_STRING_LIVE
//For Development Environment
resValue "string", "text", YOUR_STRING_DEVELOPMENT
并且可以直接在xml中使用,如
android:text="@string/text"
另一种解决方案是在您的 src 文件夹下创建 debug 和 release 文件夹,并在 debug 和 release 版本之间保留所有具有不同值的公共资源。所以你将拥有:
\src\release\res\values\strings.xml
和
<string name="your_string">release_value_here</string>
和
\src\debug\res\values\strings.xml
和
<string name="your_string">debug_value_here</string>
然后在 XML
android:text="@string/your_string"
最终我用自己的方式保持清洁。
我看过 Aspect Oriented Programming 并用 AspectJ 做了一个 Aspect。
@Aspect
class PrefillAspect {
@After("execution(* com.example.aspect.LoginActivity.onCreate(*))")
fun prefillLoginForm(joinPoint: JoinPoint) {
try {
val activity = joinPoint.target as LoginActivity
activity.findViewById<EditText>(R.id.editEmail).setText("test@example.com")
activity.findViewById<EditText>(R.id.editPassword).setText("MySecretPassword")
} catch (e: Throwable) {
Log.e("PrefillAspect", "prefillLoginForm: failed")
}
}
}
我已将此方面添加到我的 src/debug/java
文件夹中,因此此方面仅在调试版本为 运行 时应用。我的主要源代码中没有任何代码,因此永远不会发布,代码库保持干净。
我在这里写了一篇关于这个的文章:https://medium.com/@dumazy/prefill-forms-on-android-with-aspectj-97fe9b3b48ab
是否可以在 EditText
中仅针对特定的构建类型设置文本?
我希望在 运行 调试构建类型时预填充我正在开发的应用程序中的 EditText
。
我现在看到的唯一方法是以编程方式检查当前构建类型是否为 "debug" 并调用 setText()
.
我希望能够以更简洁的方式执行此操作。也许类似于 XML 布局中的 tools
命名空间。
有什么建议吗?
您可以在 buildTypes
build.gradel
文件中为不同的环境添加一些文本
//For Development Environment
buildConfigField "String", "text", "\"DEVELOPMENT ENVIRONMENT TEXT\""
//For Live Environment leave it empty
buildConfigField "String", "text", "\"\""
然后在 activity 中直接将其设置为您的编辑文本,无需手动检查任何内容。
etValue.setText(BuildConfig.text);
更优选的解决方案(直接XML)
而不是 buildConfigField
使用 resValue
,它将在项目重建时为不同的环境生成 String Resource
。
//For Live Environment leave it empty
resValue "string", "text", YOUR_STRING_LIVE
//For Development Environment
resValue "string", "text", YOUR_STRING_DEVELOPMENT
并且可以直接在xml中使用,如
android:text="@string/text"
另一种解决方案是在您的 src 文件夹下创建 debug 和 release 文件夹,并在 debug 和 release 版本之间保留所有具有不同值的公共资源。所以你将拥有:
\src\release\res\values\strings.xml
和
<string name="your_string">release_value_here</string>
和
\src\debug\res\values\strings.xml
和
<string name="your_string">debug_value_here</string>
然后在 XML
android:text="@string/your_string"
最终我用自己的方式保持清洁。 我看过 Aspect Oriented Programming 并用 AspectJ 做了一个 Aspect。
@Aspect
class PrefillAspect {
@After("execution(* com.example.aspect.LoginActivity.onCreate(*))")
fun prefillLoginForm(joinPoint: JoinPoint) {
try {
val activity = joinPoint.target as LoginActivity
activity.findViewById<EditText>(R.id.editEmail).setText("test@example.com")
activity.findViewById<EditText>(R.id.editPassword).setText("MySecretPassword")
} catch (e: Throwable) {
Log.e("PrefillAspect", "prefillLoginForm: failed")
}
}
}
我已将此方面添加到我的 src/debug/java
文件夹中,因此此方面仅在调试版本为 运行 时应用。我的主要源代码中没有任何代码,因此永远不会发布,代码库保持干净。
我在这里写了一篇关于这个的文章:https://medium.com/@dumazy/prefill-forms-on-android-with-aspectj-97fe9b3b48ab