Android - 如何在片段类中访问操作栏的菜单项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24526783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How to access actionbar's menu items in fragment class
提问by Dao Quoc Khanh
How should I access the actionbar's menu items in fragment ? I have tried this but nothing happened
我应该如何访问片段中操作栏的菜单项?我试过这个,但什么也没发生
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menu_refresh:
Toast.makeText(getActivity().getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
}
回答by joselufo
Follow this steps:
请按照以下步骤操作:
Add setHasOptionsMenu(true) method in your Fragment.
Override onCreateOptionsMenu(Menu menu, MenuInflater inflater) and onOptionsItemSelected(MenuItem item) methods in your Fragment.
Inside your onOptionsItemSelected(MenuItem item) Activity's method, make sure you return false when the menu item action would be implemented in onOptionsItemSelected(MenuItem item) Fragment's method.
在您的片段中添加 setHasOptionsMenu(true) 方法。
覆盖片段中的 onCreateOptionsMenu(Menu menu, MenuInflater inflater) 和 onOptionsItemSelected(MenuItem item) 方法。
在 onOptionsItemSelected(MenuItem item) Activity 的方法中,确保在 onOptionsItemSelected(MenuItem item) Fragment 的方法中实现菜单项操作时返回 false。
Example:
例子:
Activity
活动
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.activity_menu_item:
// Do Activity menu item stuff here
return true;
case R.id.fragment_menu_item:
// Not implemented here
return false;
default:
break;
}
return false;
}
Fragment
分段
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
....
}
@Override
public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
// Do something that differs the Activity's menu here
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.activity_menu_item:
// Not implemented here
return false;
case R.id.fragment_menu_item:
// Do Fragment menu item stuff here
return true;
default:
break;
}
return false;
}
回答by Simon
In your fragments onCreate
method add setHasOptionsMenu(true);
.
在您的片段onCreate
方法中添加setHasOptionsMenu(true);
.
If your fragment is in a ViewPager then the fragment with the ViewPager also needs the above line.
如果您的片段在 ViewPager 中,那么带有 ViewPager 的片段也需要上面的行。
回答by Jo?o Marcos
You cant access directly ActionBar menu items in a Fragment. What you can do is put setHasOptionsMenu(true); in onCreateView function in fragment class and this calls the function onCreateOptionsMenu(Menu menu) in the corresponding activity.
您无法直接访问 Fragment 中的 ActionBar 菜单项。你可以做的是 setHasOptionsMenu(true); 在片段类的 onCreateView 函数中,这会调用相应活动中的 onCreateOptionsMenu(Menu menu) 函数。
There, you can access all the menu items you have in the action bar. You can use:
在那里,您可以访问操作栏中的所有菜单项。您可以使用:
MenuItem item = menu.getItem(index);
You have one example of using this:
你有一个使用这个的例子:
in fragment onCreateView class:
在片段 onCreateView 类中:
setHasOptionsMenu(true);
in corresponding activity class:
在相应的活动类中:
@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);
MenuItem item = menu.getItem(0);
if(condition)
item.setVisible(true);
else
item.setVisible(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_logout:
makeLogout();
return true;
default :
return super.onOptionsItemSelected(item);
}
}
回答by bastien
One thing I would add to this (my reputation does not allow me to comment) and reason it was not working for me.
我要补充的一件事(我的声誉不允许我发表评论)以及它对我不起作用的原因。
Make sure your fragment's hosting activity extends AppCompatActivity
not FragmentActivity
!
确保您的片段的托管活动AppCompatActivity
不会扩展FragmentActivity
!
public class MainActivity extends AppCompatActivity {
}
From the Google Reference Documentationfor FragmentActivity:
来自FragmentActivity的 Google 参考文档:
Note: If you want to implement an activity that includes an action bar, you should instead use the ActionBarActivity class, which is a subclass of this one, so allows you to use Fragment APIs on API level 7 and higher.
注意:如果你想实现一个包含操作栏的活动,你应该改用 ActionBarActivity 类,它是这个类的子类,因此允许你在 API 级别 7 及更高级别上使用 Fragment API。
ActionBarActivity
now being deprecated, use AppCompatActivity
instead. When using AppCompatActivity
, also make sure you set "the activity theme to Theme.AppCompat
or a similar theme" (Google Doc too).
ActionBarActivity
现在已弃用,请AppCompatActivity
改用。使用时AppCompatActivity
,还要确保将“活动主题设置为Theme.AppCompat
或类似主题”(也是 Google Doc)。
android.support.v7.app.AppCompatActivity
is a subclass of the android.support.v4.app.FragmentActivity
class (see AppCompatActivityref doc).
android.support.v7.app.AppCompatActivity
是android.support.v4.app.FragmentActivity
该类的子类(请参阅AppCompatActivity参考文档)。
回答by Hasan Masud
Menu item main.xml file :
菜单项 main.xml 文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="dbl.contact.manager.MainActivity" >
<item
android:id="@+id/menu_search"
android:actionLayout="@layout/action_bar"
android:icon="@drawable/searchagain"
android:orderInCategory="0"
android:showAsAction="always"
android:title="search"/>
</menu>
This is Menu item. custom action bar like edittext in the action bar You have to create a custom layout. Here custom action_bar.xml file :
这是菜单项。自定义操作栏,如操作栏中的 edittext 您必须创建自定义布局。这里自定义 action_bar.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/inputSearch"
android:layout_width="280dp"
android:layout_height="40dp"
android:cursorVisible="true"
android:hint="Search"
android:imeOptions="actionDone"
android:inputType="text"
android:textColor="@android:color/white"
android:textCursorDrawable="@android:color/white" />
Then in the fragment class you to override. Just Copy and paste this code.
然后在片段类中覆盖。只需复制并粘贴此代码。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
getActivity().getMenuInflater().inflate(R.menu.main, menu);
View v = (View) menu.findItem(R.id.menu_search).getActionView();
inputSearch = (EditText)v.findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
// clientAdapter.getFilter().filter(s.toString());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
// ListData.this.clientAdapter.getFilter().filter(s);
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
adapter.getFilter().filter(s.toString());
}
});
}