在Android中,有没有办法要求用户从弹出菜单中选择一个选项?我不想使用对话框片段,因为我觉得它太具有阻碍性。因此,如果他们在弹出菜单外单击,菜单仍应保留在那里,直到他们选择一个选项。
public void showPopup(View view){
autonButton = (ToggleButton)view;
pw = new PopupWindow(getApplicationContext());
pw.setTouchable(true);
pw.setFocusable(true);
pw.setOutsideTouchable(true);
pw.setTouchInterceptor(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
{
//pw.dismiss();
Log.d("popup window", "popup outside");
return false;
}
else if(event.getAction() == MotionEvent.ACTION_DOWN)
Log.d("popup window", "popup down");
return true;//if I put true here, the entire screen freezes, and i can't get any input
}
});
pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
pw.setOutsideTouchable(false);
pw.setContentView(autonPopup);
pw.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
pw.showAsDropDown(view, 0, 0);
View popupView = pw.getContentView();
optionR = (Button) popupView.findViewById(R.id.auton_select1);
optionR.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
autonButton.setChecked(true);
autonButton.setText("Red");
}
});
optionB = (Button) autonPopup.findViewById(R.id.auton_select2);
optionB.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
autonButton.setChecked(true);
autonButton.setText(" Blue");
}
});
}