DatePickerFragment选中日期后传给activity的textview

Analysis:Fragment和Activity的通信

Resolution:fragment定义回调接口,activity实现具体方法


Steps:

In fragment:

1. fragment中定义接口并确保activity调用了这个接口

2. 调用接口方法传值给activity

public class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener{

    OnDateSetListener mListener;

    @Override

    // 2. onAttach() detect whether the Activity has inherited the interface,
    // if not it will throw an exception
    // Called when a fragment is first attached to its context
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnDateSetListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnDateSetListener");
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
        // 3. call method in interface to send values
        mListener.onDateSet(year, month, day);
    }

    // 1. Define a callback interface
    public interface OnDateSetListener{
        // method to be implemented in activity
        void onDateSet(int year, int month, int day);
    }

}

In Activity:

1. 调用接口

2. 实现接口方法(使用接收到的变量显示在textview)

// implement interface[DatePickerFragment.OnDateSetListener] declared in fragment
public class RegisterActivity extends AppCompatActivity implements DatePickerFragment.OnDateSetListener {

    private TextView tv_showDoB;
    private TextView tv_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        tv_showDoB = (TextView) findViewById(R.id.tv_showDoB);
        tv_login = (TextView) findViewById(R.id.tv_login);

        tv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                //start intent
                startActivity(intent);
                finish();
            }
        });
    }
    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager() , "datePicker");
    }

    // 4. Implement method in interface
    public void onDateSet(int year, int month, int day) {
        tv_showDoB.setText(year+"-"+(++month)+"-"+day);
    }
}

效果:



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值