我有一个工具栏,其典型设置活动已附加到3点菜单上。
在我的一个片段中,我更改了工具栏以添加几个图标,但是当按下这些图标时,它将运行其方法,然后启动典型的设置活动,
这是我在主要活动中如何调用设置的方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem mItem) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = mItem.getItemId();
Intent intent = new Intent(MainActivity.this, Settings.class);
startActivity(intent);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(mItem);
}
这是我添加项目并在片段中使用它们的方式
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
// Inflate the menu items for use in the action bar
inflater.inflate(R.menu.set_menu, menu);
mShare = menu.findItem(R.id.share);
mSave = menu.findItem(R.id.save);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.share:
share();
break;
case R.id.save:
saveWallpaper();
return true;
default:
}
return true;
}
我仍然是android的新手,希望这对所有帮助都显得微不足道
您onOptionsItemSelected()
无条件调用startActivity()
,而不是仅在选择“设置”选项时调用它。在if语句中移动这些行:
@Override
public boolean onOptionsItemSelected(MenuItem mItem) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = mItem.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, Settings.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(mItem);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句