gpt4 book ai didi

java - 如何测试MultiChoiceItems android对话框

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:36 25 4
gpt4 key购买 nike

我用

构建了一个 AlertDialog
  public class ConstantDialogFragment extends DialogFragment {

private AlertDialog.Builder builder;

private AlertDialog alertDialog;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final int[] constantProtocol = {0};
builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

builder.setTitle(getResources().getString(R.string.some_message))
.setMultiChoiceItems(R.array.some_choice, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// some method
}
})
.setPositiveButton(getResources().getString(R.string.ok_dialog), new DialogInterface.OnClickListener() {

class LoadConfigTask {
private ProgressDialog dialog;
private Activity activity;

public LoadConfigTask(Activity activity) {
this.activity = activity;
dialog = new ProgressDialog(activity, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
}

public void execute() {
try {
// some method

} catch (Exception e) {
// some method

}
}
}

@Override
public void onClick(DialogInterface dialog, int id) {
// some method
}
})
.setNegativeButton(getResources().getString(R.string.cancel_dialog), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// some thing
}
});
builder.create();
alertDialog = builder.show();
return alertDialog;
}

public AlertDialog getAlertDialog(){
return alertDialog;
}

然后我尝试了这个espresso实现:

onView(withText("my first choice")).
perform(click());

我得到

NoMatchingViewException: No views in hierarchy found matching: with text: is my string"

我该如何解决这个问题?

最佳答案

Android源代码中获取,我发现了有用的方法。因此,为了控制 AlertDialog 的项目,我必须这样做

ListView listView = alertDialog.getListView();
View child = listView.getChildAt(0);
child.performClick();

关于java - 如何测试MultiChoiceItems android对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com