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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:ems="10">

        <requestFocus/>
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:text="Button"/>

</LinearLayout>
  • 使用
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();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值