有没有办法在android中测试onCreateOptionsMenu(菜单菜单)?

时间:2015-03-20 10:17:37

标签: android testing oncreateoptionsmenu

我想写一个测试来验证我在onCreateOptionsMenu中做了什么 任何人都可以帮我解决这个问题吗?

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.global, menu);
        setdate(menu,this);
        return super.onCreateOptionsMenu(menu);

    }

2 个答案:

答案 0 :(得分:0)

对于测试,如果要检查是否调用setDate(),可以添加日志并在DDMS中检查它们 - > Logcat:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.global, menu);
        Log.d("tag","onCreateOptionsMenu calling setDate");
        setdate(menu,this);
        Log.d("tag","onCreateOptionsMenu called setDate");
        return super.onCreateOptionsMenu(menu);

    }

在显示屏上,将显示 R.menu.global 中存储的菜单

答案 1 :(得分:0)

在res / menu / main.xml中设置此更改

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/testMenu"
    android:title="@string/test"/>
</menu>

并在您的活动中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

    @Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.testMenu:
        //here write your action or call your method for example: setData()
        Log.i("TEST_MENU" , "Test Passed");
        break;
    default:
        break;
    }

    return super.onOptionsItemSelected(item);
}