如何知道按钮开/关点击与否

时间:2015-06-02 06:24:17

标签: java android uiautomator android-uiautomator

我正在为Android应用程序编写自动化。

要编写自动化代码,我正在使用uiautomatorviewer(android sdk工具)检查应用程序

我想查看华氏度和摄氏度的天气。

有一个按钮开关天气单位。

切换天气单位后,我怎么知道华氏度或摄氏度

以下是附加的屏幕截图

  1. 天气单位enter image description here

  2. Fahrenheit enter image description here

  3. 摄氏度enter image description here

  4. 仅供参考我不是改变移动应用程序代码的开发人员。我正在为移动应用程序编写自动化测试。有没有办法使用http://appium.io/使用java自动执行此操作?

6 个答案:

答案 0 :(得分:0)

使用开关,

<Switch
    android:id="@+id/mySwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="deg celcius"
    android:textOff="deg fah"/>

你打开= deg Celcius并关闭​​= deg Fahrenheit,所以使用setOnCheckedChangeListener

Switch switchStatus = (TextView) findViewById(R.id.switchStatus);
mySwitch = (Switch) findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) { 

       if(isChecked){ // deg celcius
           // Do operation
       }else{ // deg fahrenheit
           // Do operation
       }     
   }
});

答案 1 :(得分:0)

根据你的解释,你看起来需要一个ToggleButton Toggle Button Developer Link

如果按钮被选中,您可以通过以下方式获取:

boolean on = ((ToggleButton) view).isChecked();

一旦你有了togglebutton,在你的布局中只需设置:

 android:textOn="@string/Fahrenheit"
 android:textOff="@string/Celsius"

此按钮的OnClickListener执行您各自的逻辑。这应该足以让您前进。

答案 2 :(得分:0)

试试这个:

    YourSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
    if(isChecked){
    switchStatus.setText("Switch is currently ON");
    }else{
    switchStatus.setText("Switch is currently OFF");
    }
    }
    });

答案 3 :(得分:0)

我建议您选择Switch,但如果您想继续使用,那么您必须自己管理两个textview的状态(启用/禁用)。

TextView fehreniteTextview, celsiusTextView;
fehreniteTextview=(TextView)findViewById(R.id.fehrenite);
celsiusTextView=(TextView)findViewById(R.id.celsius);

所以在这里,onClick你将不得不改变另一个文本视图的状态,如:

public void onClickOfFahrenite(View v){
    celsiusTextView.setEnabled(true);
    fehreniteTextview.setEnabled(false);
}

public void onClickOfCelsius(View v){
    celsiusTextView.setEnabled(false);
    fehreniteTextview.setEnabled(true);
}

现在,在做出任何决定之前,您必须使用以下方法检查启用了哪个textview:

if(fehreniteTextview.isEnabled())
{
// Do your stuff
}
else
{
// Do your stuff
}

我希望,它可以帮到你!

答案 4 :(得分:0)

通过查看屏幕截图,textview的背景颜色从灰色变为蓝色。因此,如果您获得textview的背景颜色,则可以知道哪个开关处于打开状态。 您可以查看https://groups.google.com/forum/#!topic/appium-discuss/buToEAIDn2c

答案 5 :(得分:0)

这里的每个答案都来自Android开发者,而不是Android测试人员,没有人回答你的问题(他们似乎没有看到你无法改变应用程序本身)。

应用程序完成的方式你无法做到。 UiAutomator不会读取颜色或类似的东西。因此,如果这些元素的属性没有差异(除了文本和边界),它就无法知道所选择的含义。

如果应用程序正确完成,它将是一个ToogleButton,然后属性(选中或选中我确定哪个)将指示选择了哪个。

相关问题