gpt4 book ai didi

android - 如何从我的 DatePicker fragment 传输格式化的日期字符串?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:53:44 27 4
gpt4 key购买 nike

按照官方文档http://developer.android.com/guide/topics/ui/controls/pickers.html#DatePicker .

我使用了相同的代码,并在 onDateSet 方法中仅添加了结果格式:

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

@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);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());

// How to get the string from here to the caller?

}
}

为了测试,调用者 Activity 仅显示 TextView 并在用户触摸小部件时调用选择器:

public class OrderHeadEditActivity extends Activity {

private TextView mDTDelivery;
...

@Override
protected void onCreate(Bundle bundle) {
...
mDTDelivery = (TextView) findViewById(R.id.order_head_view_dt_delivery);
...
mDTDelivery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
}
...
}

显示日期选择器,在结果格式化为字符串的地方设置断点后,我可以看到它起作用了。

但是,我不知道如何将字符串值传回mtDTDelivery 小部件?

最佳答案

正如 tyczj 所指出的,您可以使用接口(interface)方法,或者您可以使数据选择器类成为 Activity 类的内部类,在这种情况下,它应该是静态的,并且您需要外部类的弱引用

使用界面。

DatePickerFragment 中定义一个接口(interface)。在您的 Activity 类中实现接口(interface)并将日期设置为 TextView 。

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

TheListener listener;

public interface TheListener{
public void returnDate(String date);
}

@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);
listener = (TheListener) getActivity();

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

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null)
{
listener.returnDate(formattedDate);

}

}
}

主要 Activity

public class MainActivity extends Activity implements DatePickerFragment.TheListener{

Button b;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
tv= (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}

});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void returnDate(String date) {
// TODO Auto-generated method stub
tv.setText(date);
}

}

第二种方式不确定这是否是最好的方式。我会推荐上面的接口(interface)方法。

public class MainActivity extends Activity {//implements DatePickerFragment.TheListener{

Button b;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b= (Button) findViewById(R.id.button1);
tv= (TextView) findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}

});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

private WeakReference<MainActivity> mActivity;

@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);
mActivity = new WeakReference<MainActivity>((MainActivity) getActivity());

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

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
MainActivity target = mActivity.get();
if (target != null) target.tv.setText(formattedDate);

}
}
}

关于android - 如何从我的 DatePicker fragment 传输格式化的日期字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18211684/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com