我有一个片段,只有一个Button
。我的目标是在用户点击该按钮时显示menu_language.xml
。我试图在网上找到一个答案,这是我能找到的最接近的答案......但它不起作用:如果我按下按钮没有任何反应。这段代码有什么问题?
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fgm = inflater.inflate(R.layout.fragment_settings, container, false);
Button changeLanguageBtn = (Button) fgm.findViewById(R.id.change_language_button);
changeLanguageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().openOptionsMenu();
}
});
return fgm;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
if (view.getId() == R.id.change_language_button) { // is this even necessary?
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.menu_language, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_english:
// switch to English
return true;
case R.id.action_italian:
// switch to Italian
return true;
case R.id.action_french:
// switch to French
return true;
default:
return super.onContextItemSelected(item);
}
}
这里是菜单的xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/action_italian"
android:title="@string/italian" />
<item
android:id="@+id/action_english"
android:title="@string/english" />
<item
android:id="@+id/action_french"
android:title="@string/french" />
</group>
</menu>
答案 0 :(得分:0)
您需要实现选项菜单,您实现了上下文菜单。
答案 1 :(得分:0)
我认为你有错误的方法(如果你想在pickStartTime = (TimePicker)findViewById(R.id.timePicker1);
pickStartTime.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
Log.d("Time", "You picked "
+ String.valueOf(view.getCurrentHour()) + ":"
+ String.valueOf(view.getCurrentMinute()));
}
});
添加一个菜单:
ActionBar
如果您阅读该方法&#39; Javadoc:
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo)
鉴于:
/**
* Called when a context menu for the {@code view} is about to be shown.
* Unlike {@link #onCreateOptionsMenu}, this will be called every
* time the context menu is about to be shown and should be populated for
* the view (or item inside the view for {@link AdapterView} subclasses,
* this can be found in the {@code menuInfo})).
* <p>
* Use {@link #onContextItemSelected(android.view.MenuItem)} to know when an
* item has been selected.
* <p>
* The default implementation calls up to
* {@link Activity#onCreateContextMenu Activity.onCreateContextMenu}, though
* you can not call this implementation if you don't want that behavior.
* <p>
* It is not safe to hold onto the context menu after this method returns.
* {@inheritDoc}
*/
说:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
因此,如果要向活动添加菜单,则必须使用后者。现在,如果您想显示一个锚定在您按下的按钮的上下文菜单,请查看Parsing values from a JSON file using Python?类。
关于您的/**
* Initialize the contents of the Activity's standard options menu. You
* should place your menu items in to <var>menu</var>. For this method
* to be called, you must have first called {@link #setHasOptionsMenu}. See
* {@link Activity#onCreateOptionsMenu(Menu) Activity.onCreateOptionsMenu}
* for more information.
*
* @param menu The options menu in which you place your items.
*
* @see #setHasOptionsMenu
* @see #onPrepareOptionsMenu
* @see #onOptionsItemSelected
*/
评论,答案是很可能不是。