Android DialogFragment使用

1. 简单使用

  • 继承DialogFragment
/**
 * 显示系统的对话框Fragment
 * @author mazaiting
 * @date 2018/3/1
 */

public class CustomDialogFragment extends DialogFragment implements DialogInterface.OnClickListener{
    private static final String TAG = "CustomDialogFragment";
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder =
                new AlertDialog.Builder(getActivity())
                .setTitle("标题")
                .setMessage("消息")
                .setPositiveButton("确定", this)
                .setNegativeButton("取消", this)
                .setCancelable(false);
        // 这里不能调用show方法
        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_NEGATIVE:
                Toast.makeText(getActivity(), "取消", Toast.LENGTH_SHORT).show();
                break;
            case DialogInterface.BUTTON_POSITIVE:
                Toast.makeText(getActivity(), "确定", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
  • 使用
new CustomDialogFragment().show(getSupportFragmentManager(), "custom");

2. 自定义View的DialogFragment

  • 继承Fragment
public class ViewDialogFragment extends DialogFragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        return inflater.inflate(R.layout.dialog, container, false);
    }
}
  • 布局dialog.xml



    

    

        
    

    
  • 使用
new ViewDialogFragment().show(getSupportFragmentManager(), "view");

3. 接口通信

  • 接口
/**
 * 接口通信
 * @author mazaiting
 * @date 2018/3/1
 */

public interface DataCallback {
    /**
     * 获取数据
     * @param data 数据
     */
    void getData(String data);
}
  • 继承DialogFragment
/**
 * 接口通信
 *
 * @author mazaiting
 * @date 2018/3/1
 */

public class DataDialogFragment extends DialogFragment implements DialogInterface.OnClickListener {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder =
                new AlertDialog.Builder(getActivity())
                        .setTitle("标题")
                        .setMessage("消息")
                        .setPositiveButton("确定", this)
                        .setNegativeButton("取消", this)
                        .setCancelable(false);
        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        DataCallback callback = (DataCallback) getActivity();
        switch (which) {
            case DialogInterface.BUTTON_NEGATIVE:
                callback.getData("取消");
                break;
            case DialogInterface.BUTTON_POSITIVE:
                callback.getData("确定");
                break;
            default:
                break;
        }
    }
}
  • 使用
public class MainActivity extends AppCompatActivity implements DataCallback{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
    }

    @OnClick({R.id.btn_custom, R.id.btn_view, R.id.btn_get_data})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_custom:
                new CustomDialogFragment().show(getSupportFragmentManager(), "custom");
                break;
            case R.id.btn_view:
                new ViewDialogFragment().show(getSupportFragmentManager(), "view");
                break;
            case R.id.btn_get_data:
                new DataDialogFragment().show(getSupportFragmentManager(), "data");
                break;
            default:
                break;
        }
    }

    @Override
    public void getData(String data) {
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
    }
}

你可能感兴趣的:(Android DialogFragment使用)