本文共 3974 字,大约阅读时间需要 13 分钟。
/** * 显示系统的对话框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");
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); }}
new ViewDialogFragment().show(getSupportFragmentManager(), "view");
/** * 接口通信 * @author mazaiting * @date 2018/3/1 */public interface DataCallback { /** * 获取数据 * @param data 数据 */ void getData(String data);}
/** * 接口通信 * * @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(); }}
转载地址:http://tolhx.baihongyu.com/