java - 检查没有 getInputType 的自定义视图的输入类型
问题描述
我的问题是关于 Android/Java 的。
如何在不创建 attr.xml 的情况下检查自定义视图的输入类型?
我的 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:inputType="textEmailAddress"
android:layout_height="wrap_content"
android:ems="10"/>
<org.javaforum.input
android:layout_width="wrap_content"
android:inputType="textEmailAddress"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter your E-Mail"
/>
</LinearLayout>
我的输入.java:
public class input extends TextView{
public input(Context context, AttributeSet attr) {
super(context, attr, getIdentifier(attr));
}
public static int getIdentifier(AttributeSet attr){
//How can check if input type are textEmailAddress?
}
@Override
public void onFinishInflate() {
}
}
所以我想知道我的自定义视图的输入类型是否设置为“textEmailAddress”。我怎样才能做到这一点?我不能使用 getInputType 方法,因为在我的情况下,对象尚未初始化。如果没有“getInputType”方法,如何解决我的问题?
解决方案
public class Input extends TextView {
private static final int[] INPUT_TYPE_ATTR = new int[]{android.R.attr.inputType};
public Input(Context context, AttributeSet attr) {
super(context, attr, getIdentifier(context, attr));
}
public static int getIdentifier(Context context, AttributeSet attr) {
final TypedArray a = context.obtainStyledAttributes(attr, INPUT_TYPE_ATTR);
try {
final int inputType = a.getInt(0, 0);
if (inputType == (EditorInfo.TYPE_CLASS_TEXT |
EditorInfo.TYPE_TEXT_VARIATION_EMAIL_ADDRESS)) {
// your logic here
}
} finally {
a.recycle();
}
return 0;
}
@Override
public void onFinishInflate() {
super.onFinishInflate();
}
}
推荐阅读
- wordpress - 当我激活我的插件时,会显示此消息:此网站出现严重错误
- android - 设备重新启动后,警报管理器在给定时间之前启动
- apache - .htaccess php 中的重定向规则
- c# - 例如,是否可以在手机上打开游戏,或者在我的应用程序中进入图库?
- assembly - 无法弄清楚为什么我在 YouTube 上找到的 MIPS 汇编语言代码中遇到错误
- ios - 如何在IOS上通过硬件播放解码的音频数据?
- javascript - 如何在 JavaScript 中获取创建的音频元素的文件大小?
- node.js - 使用 Dgraph:在保存之前将操作暴露给前端并处理突变
- reactjs - Reactjs 中的 Unicode 表情符号渲染
- pandas - plot_recursive_coefficient statsmodels 创建子图