我正在开一个巴士预订应用程序。
这是我的主页:
随时点击JournyDate布局将打开此页面:
基本上,我需要在JournyDate布局上设置选定的日期。
对于日历,我使用的是this library。
Home Fragment
datelayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), DateChooser.class);
startActivityForResult(intent, 11);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == 11) {
Date date = new Date(data.getExtras().getString("Date"));
txtDate.setText((CharSequence) date);
}
}
DateChooser活动
public class DateChooser extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date_chooser);
MaterialCalendarView calendarView = (MaterialCalendarView) findViewById(R.id.calendarView);
CalendarDay calendarDay = calendarView.getCurrentDate();
calendarView.setCurrentDate(calendarDay);
Date date = calendarDay.getDate();
calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
Intent result = new Intent();
result.putExtra("Date", date);
setResult(RESULT_OK, result);
finish();
}
});
}
}
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smiles.bus, PID: 19691
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65547, result=-1, data=Intent { (has extras) }} to activity {com.smiles.bus/com.smiles.bus.MainActivity}: java.lang.IllegalArgumentException: The string argument is null
at android.app.ActivityThread.deliverResults(ActivityThread.java:4998)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
at android.app.ActivityThread.access$1600(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.IllegalArgumentException: The string argument is null
at java.util.Date.parse(Date.java:384)
at java.util.Date.<init>(Date.java:157)
at com.smiles.bus.Home.onActivityResult(Home.java:129)
at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
at android.app.Activity.dispatchActivityResult(Activity.java:7165)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4994)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
at android.app.ActivityThread.access$1600(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
如果您希望将DateChooser作为活动启动,请使用以下命令启动它:
Intent intent = new Intent(this, DateChooser.class);
startActivityForResult(intent, REQUEST_DATE);
并对结果实施反应:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == REQUEST_DATE) {
Date date = new Date(data.getExtras().getString(EXTRA_DATE));
...
}
}
将结果返回DateChooser,如下所示:
Intent result = new Intent();
result.putExtra(EXTRA_DATE, date);
setResult(RESULT_OK, result);
finish();
将EXTRA_DATE
定义为字符串常量,将REQUEST_DATE
定义为int constant。
我没有测试过日期值的正确转换。应该只是一个灵感。
答案 1 :(得分:0)
答案 2 :(得分:0)
当您选择任何日期时将该日期转换为String变量,如下面的代码..
像这样转换日期对象字符串..
String selectedDate=date.toString();
Intent result = new Intent(currentclassName.this,NextActivity.class);// if you finish then no need to define parameters into intent.
result.putExtra("Date", selectedDate);
setResult(RESULT_OK, result);
finish();
如果传入片段,则使用参数传递数据。
答案 3 :(得分:0)
使用 public class Easy extends javax.swing.JFrame {
/**
* @param args
* the command line arguments
*/
public static void main(final String args[]) {
/* Set the Nimbus look and feel */
// <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. For details see
* http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (final javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (final ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Easy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (final InstantiationException ex) {
java.util.logging.Logger.getLogger(Easy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (final IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Easy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (final javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Easy.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
// </editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(() -> new Easy().setVisible(true));
}
NewTimer time;
// Variables declaration - do not modify
public javax.swing.JButton btnStart;
public javax.swing.JPanel jPanel1;
public javax.swing.JLabel lblTimerE;
public javax.swing.JLabel lblWordE;
// End of variables declaration
/**
* Creates new form Easy
*/
public Easy() {
initComponents();
}
private void btnStartActionPerformed(final java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
time.startTimer();
}
/**
* This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form
* Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
lblTimerE = new javax.swing.JLabel();
btnStart = new javax.swing.JButton();
lblWordE = new javax.swing.JLabel();
time = new NewTimer(lblTimerE);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
// Rest of your code...
}// </editor-fold>
}
public class NewTimer {
JLabel lblTimerE;
Timer timer = null;
int counter = 5;
int delay = 1500;
public NewTimer(final JLabel lbl) {
lblTimerE = lbl;
}
// method that will be called when the timer needs to start
public void startTimer() {
final ActionListener action = new ActionListener() {
@Override
public void actionPerformed(final ActionEvent event) {
if (counter == 0) {
timer.stop();
lblTimerE.setText("Time up");
} else {
lblTimerE.setText("Wait for " + counter + " sec");
counter--;
}
}
}; // end of action performed statement
timer = new Timer(delay, action);
timer.setInitialDelay(0);
timer.start(); // this will start the timer
} // end of method
}
代替活动。
尝试在DialogFragment
或其他内容中添加此MyDatePickerFragment
。
MainActivity
从你的onclick()
调用片段public class MyDatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
return datePickerDialog;
}
private DatePickerDialog.OnDateSetListener dateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int month, int day) {
String selectedDate = view.getDayOfMonth() + "/" + (view.getMonth() + 1) + "/" + view.getYear();
txtDate.setText(selectedDate);
}
};
}