如何正确使用clickView?
我正在尝试编写一个简单的测试应用程序以在 JUnit 中运行。我一直无法让 clickView 单击正确的视图。以下是一些可在 SDK 附带的 SkeletonActivity 示例应用程序上使用的示例代码。
public class SkeletonInstrumentation extends ActivityInstrumentationTestCase2<Activity>{
private Activity act;
private Button bClear;
private Button bBack;
private EditText eMain;
public SkeletonInstrumentation(){
super("com.example.android.skeletonapp", Activity.class);
}
public void setUp() {
String app = this.getInstrumentation().getTargetContext().getPackageName();
this.setActivityInitialTouchMode(true);
act = this.launchActivity(app, SkeletonActivity.class, Bundle.EMPTY);
bClear = (Button) act.findViewById(R.id.clear);
bBack = (Button) act.findViewById(R.id.back);
eMain = (EditText) act.findViewById(R.id.editor);
}
public void testClick()
{
TouchUtils.clickView(this, bClear);
}
public void testSendKeys()
{
act.runOnUiThread(
new Runnable(){
public void run(){
bClear.clearFocus();
eMain.requestFocus();
}
}
);
this.sendKeys("A B C D E F G ENTER");
}
}
testClick 运行,但在 Activity 启动时单击具有焦点的主 EditText 视图,并最终调出屏幕键盘。我希望它单击 EditText 下方的“清除”按钮。有人能告诉我这里出了什么问题吗?
I'm trying to write a simple test app to run in JUnit. I've been having trouble getting clickView to click on the proper view. Below is some sample code that can be used on SkeletonActivity sample app that comes with the SDK.
public class SkeletonInstrumentation extends ActivityInstrumentationTestCase2<Activity>{
private Activity act;
private Button bClear;
private Button bBack;
private EditText eMain;
public SkeletonInstrumentation(){
super("com.example.android.skeletonapp", Activity.class);
}
public void setUp() {
String app = this.getInstrumentation().getTargetContext().getPackageName();
this.setActivityInitialTouchMode(true);
act = this.launchActivity(app, SkeletonActivity.class, Bundle.EMPTY);
bClear = (Button) act.findViewById(R.id.clear);
bBack = (Button) act.findViewById(R.id.back);
eMain = (EditText) act.findViewById(R.id.editor);
}
public void testClick()
{
TouchUtils.clickView(this, bClear);
}
public void testSendKeys()
{
act.runOnUiThread(
new Runnable(){
public void run(){
bClear.clearFocus();
eMain.requestFocus();
}
}
);
this.sendKeys("A B C D E F G ENTER");
}
}
testClick runs, but clicks on the main EditText view that has focus when the Activity starts and ends up bringing up the on-screen keyboard. I want it to click on the 'Clear' button below the EditText. Can someone tell me what's wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 AndroidManifest.xml 中添加最小和最大目标 SDK 对我有用;我从这个答案中得到了这个想法。在正在测试的项目(不是测试项目)中,我将以下行添加到 AndroidManifest.xml:
并且我对 TouchUtils 的所有使用再次开始表现一致。
我在这个问题中遇到了同样的问题,虽然我能够找到解决方法,但它变得非常麻烦,所以我花了更多时间追根究底。
Adding the min and max target SDK in the AndroidManifest.xml worked for me; I got the idea from this answer. In the project that is being tested (not the test project) I added the following line to AndroidManifest.xml:
and all of my uses of TouchUtils started behaving consistently again.
I had the same problem in this question and while I was able to find a workaround but it became really cumbersome so I spent more hours getting to the bottom of it.
这里clickView的使用是正确的。问题出在 AndroidManifest.xml 中。必须设置 Min 和 Target SDK 才能正常工作。
The use of clickView here is correct. The problem was in the AndroidManifest.xml. Min and Target SDK must be set for it to work properly.