在android开发中,Dialog时经常用到的,特别是自定义的Dialog,知道如何使用时很重要的.下面来看看如何实现.
最近开发照相机时候需要用到一个dialog,如下效果:
当点击菜单项"语音拍照"时候,就会弹出这个弹出框,点击这个弹出框的一个菜单项就会播放对应的声音:点击"茄子",就会听到播放茄子的声音.
在activity中,实现一个Dialog,我们可以从写activity中的方法:public Dialog onCreateDialog(int dialogId),这里的dialogId标识了我们这一次需要显示的Dialog.当然需要显示那一个Dialog在你的activity中调用这个方法:showDialog(DIALOG_ID_VOICE_COMMAND_SHOW_TONES);如果这个Dialog没有创建,就会自动回调onCreateDialog来创建我们的Dialog来显示.看看如下的实现方式:
public void VoiceCommandClickListener(View v) { long startTime = System.nanoTime(); showDialog(DIALOG_ID_VOICE_COMMAND_SHOW_TONES); /* FragmentTransaction ft = getFragmentManager().beginTransaction(); VoiceCommandDialogFragment prev =(VoiceCommandDialogFragment) Fragment.instantiate(v.getContext(), VoiceCommandDialogFragment.class.getName()); prev.show(ft, "voice_dialog"); */ long consumingTime = System.nanoTime() - startTime; Log.d("yu_camera", "VoiceCommandClickListener consumingTime="+consumingTime); } @Override public Dialog onCreateDialog(int dialogId) { if(dialogId == DIALOG_ID_VOICE_COMMAND_SHOW_TONES){ Log.d("yu_camera", "onCreateDialog "); Dialog dialog = new Dialog(this, R.style.transparent_dialog_them); dialog.setContentView(R.layout.setting_switch_sublist_layout); VoiceManager voice_manager = ((CameraApp)PhotoPreferenceActivity.this.getApplication()).getVoiceManager(); SettingSwitchSublistLayout mVoiceSettingLayout =(SettingSwitchSublistLayout) dialog.findViewById(R.id.SettingSwitchSublistLayout_ID); mVoiceSettingLayout.initialize(voice_manager.getVoiceEntryValues()); mVoiceSettingLayout.setSettingChangedListener(this); dialog.getWindow().setCloseOnTouchOutside(true); dialog.setCanceledOnTouchOutside(true); return dialog; } return super.onCreateDialog(dialogId); }其实实现起来很方便,主要是我们的activity可以帮我们管理需要显示的Dialog,所以只需要从写onCreateDialog,显示的时候调用showDialog就可以来.
不过问题来啦: 打开Activity.java去查看一下activity中对showDialog这个方法的说明,你就会发现,其实现在同样的情况时推荐使用DialogFragment来实现了.
* <em>If you are targeting {@link android.os.Build.VERSION_CODES#HONEYCOMB} * or later, consider instead using a {@link DialogFragment} instead.</em>看了上面的说明,我们就不得不优先使用 DialogFragment了.其实这就跟使用Fragment一样.把上面使用DialogFragment的注释代码打开后VoiceCommandClickListener方法变为如下:
public void VoiceCommandClickListener(View v) { long startTime = System.nanoTime(); //showDialog(DIALOG_ID_VOICE_COMMAND_SHOW_TONES); FragmentTransaction ft = getFragmentManager().beginTransaction(); VoiceCommandDialogFragment prev =(VoiceCommandDialogFragment) Fragment.instantiate(v.getContext(), VoiceCommandDialogFragment.class.getName()); prev.show(ft, "voice_dialog"); long consumingTime = System.nanoTime() - startTime; Log.d("yu_camera", "VoiceCommandClickListener consumingTime="+consumingTime); }
(1)对于Fragment的使用,一般都是通过FragmentTransaction来处理的.FragmentTransaction其实是使用者和FragmentManager的桥梁.
(2)对于Fragment的实例化,一般采用Fragment的静态方法instantiate来实现.
对于VoiceCommandDialogFragment主要是从写他的onCreateView来确定显示的内容:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.setting_switch_sublist_layout, container, false); VoiceManager voice_manager = ((CameraApp)getActivity().getApplication()).getVoiceManager(); SettingSwitchSublistLayout mVoiceSettingLayout =(SettingSwitchSublistLayout) v.findViewById(R.id.SettingSwitchSublistLayout_ID); mVoiceSettingLayout.initialize(voice_manager.getVoiceEntryValues()); mVoiceSettingLayout.setSettingChangedListener((PhotoPreferenceActivity)getActivity()); return v; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Pick a style based on the num. int style = DialogFragment.STYLE_NO_TITLE, theme = R.style.transparent_dialog_them; setStyle(style, theme); }
原文地址:http://blog.csdn.net/green_shing/article/details/45643083