在PopupMenu中不调用onOptionsItemSelected

时间:2017-05-17 15:36:21

标签: java android android-dialogfragment android-menu popupmenu

我在按下Dialog Fragment的一个元素时尝试打开弹出菜单,但是即使按下其中一个菜单项,也不会调用onOptionsItemSelected方法。我该怎么做才能解决它?

enter image description here enter image description here

public class AddSongDialogFragment extends DialogFragment implements View.OnClickListener {

private TextView genreTextview;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.addsong_layout, container);
    //...
    genreTextview = (TextView) view.findViewById(R.id.genreTextView);
    view.findViewById(R.id.ll_genre_menu).setOnClickListener(this);
    genreTextview.setText(R.string.press_to_choose_genre);
    return view;
}

//...

public void onClick(View v){
    switch(v.getId()) {
        case R.id.button2:
            dismiss();
            mListener.onChoose();
            break;
        case R.id.ll_genre_menu:
            PopupMenu popup = new PopupMenu(getContext(), v);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.song_genres, popup.getMenu());
            popup.show();
            break;
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    genreTextview.setText(item.getTitle());
    Log.d("songo", "item selected");
    return true;
}

song_genres.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/pop"
        android:title="@string/pop"/>
    <item android:id="@+id/rock"
        android:title="@string/rock" />
    ...
</menu>

1 个答案:

答案 0 :(得分:3)

您应为Private Sub UserForm_Initialize() ' *** Load the companies into the delivering firm combo box *** For Each cell In Range("RejectTitle") If cell <> "" Then Me.RejectTitleNm.AddItem cell End If Next cell End Sub

设置Private Sub RejectTitleNm_Change() Me.TextBox1.Text = Worksheets("Sheet1").Range("E5").Value End Sub

如下所示

setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener listener)