日期选择器
程序员文章站
2022-05-01 17:01:14
...
依赖
implementation 'com.contrarywind:Android-PickerView:4.1.6'
package com.example.yanhengcun.mvp.ui.fragment;
import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.example.yanhengcun.R;
import com.example.yanhengcun.di.component.DaggerMyComponent;
import com.example.yanhengcun.di.module.MyModule;
import com.example.yanhengcun.mvp.contract.MyContract;
import com.example.yanhengcun.mvp.presenter.MyPresenter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import static com.jess.arms.utils.Preconditions.checkNotNull;
public class MyFragment extends BaseFragment<MyPresenter> implements MyContract.View {
@BindView(R.id.but_dain)
Button butDain;
@BindView(R.id.tv_tv)
TextView tvTv;
Unbinder unbinder;
private TimePickerView pickerView;
public static MyFragment newInstance() {
MyFragment fragment = new MyFragment();
return fragment;
}
@Override
public void setupFragmentComponent(@NonNull AppComponent appComponent) {
DaggerMyComponent //如找不到该类,请编译一下项目
.builder()
.appComponent(appComponent)
.myModule(new MyModule(this))
.build()
.inject(this);
}
@Override
public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_my, container, false);
}
@Override
public void initData(@Nullable Bundle savedInstanceState) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, 0);
}
pickerView = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
tvTv.setText(getTime(date));
}
}).build();
}
private String getTime(Date date){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.format(date);
}
@Override
public void setData(@Nullable Object data) {
}
@Override
public void showLoading() {
}
@Override
public void hideLoading() {
}
@Override
public void showMessage(@NonNull String message) {
checkNotNull(message);
ArmsUtils.snackbarText(message);
}
@Override
public void launchActivity(@NonNull Intent intent) {
checkNotNull(intent);
ArmsUtils.startActivity(intent);
}
@Override
public void killMyself() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO: inflate a fragment view
View rootView = super.onCreateView(inflater, container, savedInstanceState);
unbinder = ButterKnife.bind(this, rootView);
return rootView;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@OnClick(R.id.but_dain)
public void onViewClicked() {
pickerView.show();
}
}