如何正确使用clickView?

发布于 2024-10-07 14:12:57 字数 1216 浏览 2 评论 0原文

我正在尝试编写一个简单的测试应用程序以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老子叫无熙 2024-10-14 14:12:57

在 AndroidManifest.xml 中添加最小和最大目标 SDK 对我有用;我从这个答案中得到了这个想法。在正在测试的项目(不是测试项目)中,我将以下行添加到 AndroidManifest.xml:

<uses-sdk android:targetSdkVersion="17" android:minSdkVersion="17"/>

并且我对 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:

<uses-sdk android:targetSdkVersion="17" android:minSdkVersion="17"/>

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.

南汐寒笙箫 2024-10-14 14:12:57

这里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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文