Android 4.0设置Dialog点击屏幕不消失

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:38   1146   0

业务的需求是变化莫测的,最近就遇到一个需求是——用户只有点击Dialog的取消按钮才会消失,点击屏幕的时候不消失。Android ICS对UI做了很大的变动,系统提倡使用DialogFragment,但是系统默认的操作习惯是点击屏幕Dialog会自动消失。

为了实现业务的需求,想过使用Dialog风格的Activity,但是做出来的效果和系统的UI效果不匹配,最终只有失败告终。在黔驴技穷的时候,决定再仔细撸一下Android文档,终于在文档中发现了Dialog的setCanceledOnTouchOutside属性,具体使用如下:

方法一:

public class MyAlertDialogFragment extends DialogFragment {

    public static MyAlertDialogFragment newInstance(int title) {
        MyAlertDialogFragment frag = new MyAlertDialogFragment();
        Bundle args = new Bundle();
        args.putInt("title", title);
        frag.setArguments(args);
        return frag;
    }

    @TargetApi(11)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");

        
        AlertDialog dialog = 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();
        dialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        
        return dialog;
    }
}

方法二:

在oncreate()方法中设置Dialog点击屏幕不可取消,实例代码如下:

   @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的第二种方式吧,第一种方式上面已经叙述了,在此就不再叙述了,直接看第二种实现的具体方式,具体代码如下所示:

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)
 {
  PromptDialogFragment pdf = new PromptDialogFragment();
  Bundle bundle = new Bundle();
  bundle.putString("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(
          R.layout.prompt_dialog, container, false);

        TextView tv = (TextView)v.findViewById(
                             R.id.promptmessage);
        tv.setText(getArguments().getString("prompt"));

        Button dismissBtn = (Button)v.findViewById(
                                 R.id.btn_dismiss);
        dismissBtn.setOnClickListener(this);

        Button saveBtn = (Button)v.findViewById(
                              R.id.btn_save);
        saveBtn.setOnClickListener(this);

        Button helpBtn = (Button)v.findViewById(
                R.id.btn_help);
        helpBtn.setOnClickListener(this);

        et = (EditText)v.findViewById(R.id.inputtext);
        if(icicle != null)
            et.setText(icicle.getCharSequence("input"));
        return v;
    }

    @Override
    public void onSaveInstanceState(Bundle icicle) {
     icicle.putCharSequence("input", et.getText());
     super.onPause();
    }

    @Override
    public void onCancel(DialogInterface di) {
     Log.v(MainActivity.LOGTAG, "in onCancel() of PDF");
     super.onCancel(di);
    }

    @Override
    public void onDismiss(DialogInterface di) {
     Log.v(MainActivity.LOGTAG, "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);
   act.onDialogDone(this.getTag(), false, tv.getText());
   dismiss();
   return;
  }
  if (v.getId() == R.id.btn_dismiss)
  {
   act.onDialogDone(this.getTag(), true, null);
   dismiss();
   return;
  }
  if (v.getId() == R.id.btn_help)
  {
   FragmentTransaction ft = getFragmentManager().beginTransaction();
      ft.remove(this);

      // in this case, we want to show the help text, but
      // come back to the previous dialog when we're done
      ft.addToBackStack(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的两种创建方式,希望对大家有所帮助。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP