1、导入依赖:
//日期选择器
implementation 'com.contrarywind:Android-PickerView:3.2.6'
2.清单文件中的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3.使用
public class MyFragment extends Fragment implements IView {
@BindView(R.id.user_img)
ImageView userImg;
@BindView(R.id.user_name)
TextView userName;
Unbinder unbinder;
@BindView(R.id.dateselect)
TextView dateselect;
private IPersenterImpl iPersenter;
private int pid;
private AlertDialog dialog;
private String path = Environment.getExternalStorageDirectory() + "/img.png";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfragment, container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
EventBus.getDefault().register(this);
iPersenter = new IPersenterImpl(this);
iPersenter.showRequestData(String.format(UrlApis.USER, pid), UserBean.class);
userImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAlert();
}
});
dateselect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TimePickerView pickerView = new TimePickerView.Builder(getActivity(), new TimePickerView.OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(date);
dateselect.setText(time+"");
}
})
.setType(new boolean[]{true,true,true,false,false,false})
.setCancelText("取消")
.setSubmitText("确定")
.isCenterLabel(true)
.build();
pickerView.show();
}
});
}
private void getAlert() {
View view = View.inflate(getActivity(), R.layout.alert_item, null);
dialog = new AlertDialog.Builder(getContext()).setView(view).show();
TextView pick = view.findViewById(R.id.pick);
TextView camera = view.findViewById(R.id.camera);
//相机
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//隐式跳转到相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
startActivityForResult(intent, 1);
dialog.dismiss();
}
});
//相册
pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 2);
dialog.dismiss();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(new File(path)), "image/*");
intent.putExtra("CROP", true);
intent.putExtra("aspectX", 1);
intent.putExtra("acpectY", 1);
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}
if (requestCode == 2 && resultCode == RESULT_OK) {
Intent intent = new Intent("com.android.camera.action.CROP");
Uri uri = data.getData();
intent.setDataAndType(uri, "image/*");
intent.putExtra("CROP", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 100);
intent.putExtra("outputY", 100);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}
if (requestCode == 3 && resultCode == RESULT_OK) {
Bitmap bitmap = data.getParcelableExtra("data");
userImg.setImageBitmap(bitmap);
}
}
@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void getData(IntentBean bean) {
if (bean.getId() == 66) {
pid = bean.getPid();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
iPersenter.onDestory();
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
public void startRequestData(Object data) {
if (data instanceof UserBean) {
UserBean bean = (UserBean) data;
String icon = (String) bean.getData().getIcon();
/*userImg.setImageURI(icon);*/
userName.setText(bean.getData().getUsername());
}
}
}