我受到以下代码的启发:
@Test
public void onType() throws Exception {
...
onView(isAssignableFrom(EditText.class)).perform(typeText("a"));
...
}
如何在活动中使用视图类查看视图,我认为控制第3个自定义视图很有用
答案 0 :(得分:0)
您所说的代码是Espresso testing framework的一部分。它用于仪器UI测试,但它不是简单应用程序的常见用法。
您甚至可以为您的应用程序(不是测试模块)设置此库的依赖关系,如下所示:
dependencies {
compile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
但我认为这些方法对性能有不良影响(可能使用简单的视图迭代)。
答案 1 :(得分:0)
如果您有某个类的对象引用,例如从View.class派生的示例类
View someView <- by findViewById(int), getView(), getRootView() etc // any method that return view
您可以检查是否可以通过向上演员将其分配给其他类 - 通过这种方式从右向左移动(下面的一些示例):
EditText <- TextView <- View
FrameLayout <- ViewGroup <- View
NumberPicker <- LinearLayou <- ViewGroup <- View
示例应用程序:
// get View class reference by call to find view by id
View view = findViewById(R.id.someView);
if(view!=null) {
Class viewClass = view.getClass()
// check if we can assign edit text class to this class ???
if(EditText.class.isAssingableFrom(viewClass)) {
// u can cast it to edit text and do some stuff with edit text class as we did up casting while in this case it is allowed:
EditText editText = (EditText) view;
editText.setText(String);
}
}
这样我们就不会出现异常,例如我们在没有任何检查的情况下进行此类演员:
((EditText) view).setText(String);
如果视图类是其他类型的EditText,或者不是从编辑文本类派生的,则会抛出异常。
来自类实现:
public final class Class<T> implements Serializable, AnnotatedElement, GenericDeclaration, Type {
...
/**
* Can {@code c} be assigned to this class? For example, String can be assigned to Object
* (by an upcast), however, an Object cannot be assigned to a String as a potentially exception
* throwing downcast would be necessary. Similarly for interfaces, a class that implements (or
* an interface that extends) another can be assigned to its parent, but not vice-versa. All
* Classes may assign to themselves. Classes for primitive types may not assign to each other.
*
* @param c the class to check.
* @return {@code true} if {@code c} can be assigned to the class
* represented by this {@code Class}; {@code false} otherwise.
* @throws NullPointerException if {@code c} is {@code null}.
*/
public boolean isAssignableFrom(Class<?> c) {
另见:
答案 2 :(得分:0)
以下代码应该有效:
static class FindV {
Context ctx;
ViewGroup vg;
Class<? extends View> targetK;
public FindV(ViewGroup vg, Class<? extends View> targetK) {
ctx = vg.getContext();
this.vg = vg;
this.targetK = targetK;
}
ArrayList<View> vs = new ArrayList<View>();
// public static <T extends View> ArrayList<T> findV(ViewGroup vg, Class<T> targetK) {
ArrayList<View> st() {
st(vg);
return vs;
}
void st(ViewGroup vg) {
int cc = vg.getChildCount();
if (cc > 0) {
for (int i = 0; i < cc; i++) {
View v = vg.getChildAt(i);
if (v.getClass() == targetK) {
vs.add(v);
}
}
if (vs.isEmpty()) {
for (int i = 0; i < cc; i++) {
View v = vg.getChildAt(i);
if (ancestorKs(v).contains(targetK)) {
vs.add(v);
}
}
}
for (int i = 0; i < cc; i++) {
View v = vg.getChildAt(i);
if (v instanceof ViewGroup) {
st((ViewGroup) v);
}
}
}
}
}
public static <T extends View> ArrayList<T> findV(ViewGroup vg, Class<T> targetK) {
return (ArrayList<T>) new FindV(vg, targetK).st();
}