业务的需求是变化莫测的,最近就遇到一个需求是——用户只有点击Dialog的取消按钮才会消失,点击屏幕的时候不消失。Android ICS对UI做了很大的变动,系统提倡使用DialogFragment,但是系统默认的操作习惯是点击屏幕Dialog会自动消失。
setCanceledOnTouchOutside属性,具体使用如下:
方法一:
[java] view plain copy
- public class MyAlertDialogFragment extends DialogFragment {
- public static MyAlertDialogFragment newInstance(int title) {
- new MyAlertDialogFragment();
- new Bundle();
- "title", title);
- frag.setArguments(args);
- return frag;
- }
- @TargetApi(11)
- @Override
- public Dialog onCreateDialog(Bundle savedInstanceState) {
- int title = getArguments().getInt("title");
- new AlertDialog.Builder(getActivity())
- .setIcon(R.drawable.ic_launcher)
- .setTitle(title)
- .setPositiveButton(R.string.alert_dialog_ok,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- ((MainActivity)getActivity()).doPositiveClick();
- }
- }
- )
- .setNegativeButton(R.string.alert_dialog_cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- ((MainActivity)getActivity()).doNegativeClick();
- }
- }
- )
- .create();
- false);// 设置点击屏幕Dialog不消失
- return dialog;
- }
- }
方法二:
在oncreate()方法中设置Dialog点击屏幕不可取消,实例代码如下:
[java] view plain copy
- @Override
- public void onCreate(Bundle icicle)
- {
- super.onCreate(icicle);
- this.setCancelable(false);// 设置点击屏幕Dialog不消失
- int style = DialogFragment.STYLE_NORMAL, theme = 0;
- setStyle(style,theme);
- }
(提示:提醒大家一下在覆写了onCreateDialog()方法后,就不能覆写onCreateView()方法了)
说到这儿就给大家介绍一下创建DialogFragment的第二种方式吧,第一种方式上面已经叙述了,在此就不再叙述了,直接看第二种实现的具体方式,具体代码如下所示:
[java] view plain copy
- import android.app.Activity;
- import android.app.DialogFragment;
- import android.app.FragmentTransaction;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- public class PromptDialogFragment
- extends DialogFragment
- implements View.OnClickListener
- {
- private EditText et;
- public static PromptDialogFragment
- newInstance(String prompt)
- {
- new PromptDialogFragment();
- new Bundle();
- "prompt",prompt);
- pdf.setArguments(bundle);
- return pdf;
- }
- @Override
- public void onAttach(Activity act) {
- // If the activity we're being attached to has
- // not implemented the OnDialogDoneListener
- // interface, the following line will throw a
- // ClassCastException. This is the earliest we
- // can test if we have a well-behaved activity.
- OnDialogDoneListener test = (OnDialogDoneListener)act;
- super.onAttach(act);
- }
- @Override
- public void onCreate(Bundle icicle)
- {
- super.onCreate(icicle);
- this.setCancelable(true);
- int style = DialogFragment.STYLE_NORMAL, theme = 0;
- setStyle(style,theme);
- }
- // 覆写onCreateView()方法,实现DialogFragment的布局。注意不能同时覆写 onCreateView()和onCreateDialog()方法
- public View onCreateView(LayoutInflater inflater,
- ViewGroup container,
- Bundle icicle)
- {
- View v = inflater.inflate(
- false);
- TextView tv = (TextView)v.findViewById(
- R.id.promptmessage);
- "prompt"));
- Button dismissBtn = (Button)v.findViewById(
- R.id.btn_dismiss);
- this);
- Button saveBtn = (Button)v.findViewById(
- R.id.btn_save);
- this);
- Button helpBtn = (Button)v.findViewById(
- R.id.btn_help);
- this);
- et = (EditText)v.findViewById(R.id.inputtext);
- if(icicle != null)
- "input"));
- return v;
- }
- @Override
- public void onSaveInstanceState(Bundle icicle) {
- "input", et.getText());
- super.onPause();
- }
- @Override
- public void onCancel(DialogInterface di) {
- "in onCancel() of PDF");
- super.onCancel(di);
- }
- @Override
- public void onDismiss(DialogInterface di) {
- "in onDismiss() of PDF");
- super.onDismiss(di);
- }
- public void onClick(View v)
- {
- OnDialogDoneListener act = (OnDialogDoneListener)getActivity();
- if (v.getId() == R.id.btn_save)
- {
- TextView tv = (TextView)getView().findViewById(R.id.inputtext);
- this.getTag(), false, tv.getText());
- dismiss();
- return;
- }
- if (v.getId() == R.id.btn_dismiss)
- {
- this.getTag(), true, null);
- dismiss();
- return;
- }
- if (v.getId() == R.id.btn_help)
- {
- FragmentTransaction ft = getFragmentManager().beginTransaction();
- this);
- // in this case, we want to show the help text, but
- // come back to the previous dialog when we're done
- null);
- //null represents no name for the back stack transaction
- HelpDialogFragment hdf =
- HelpDialogFragment.newInstance(R.string.help1);
- hdf.show(ft, MainActivity.HELP_DIALOG_TAG);
- return;
- }
- }
- }
代码比较简单,注释已经写明白了,相信大家都能看懂的!
以上只是设置Dialog的一个小技巧以及创建DialogFragment的两种创建方式,希望对大家有所帮助