我是Android Studio的新手,我正在构建一个简单的倒数计时器应用程序。我有2个活动。用户通过日期选择器选择日期的第一个活动(登录),第二个活动(个人资料)显示倒数计时器。当我在java类中为它设置日期时,倒数计时器工作正常,但我在尝试从登录活动中检索日期时遇到了麻烦。我的这两项活动的代码如下。
我知道我必须改变Date futureDate = dateFormat.parse(“2016-8-10”);但我不知道怎么做。
LOGIN.CLASS
public class Login extends AppCompatActivity implements DatePickerDialog.OnDateSetListener, OnClickListener {
//private SharedPreferences sp;
EditText enterusername;
Button continuetoprofile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
/*sp = getSharedPreferences("myPreference" , Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("myKey" , "I am data to store");
editor.commit();
Writing data to SharedPreferences*/
enterusername = (EditText) findViewById(R.id.enterusername);
continuetoprofile=(Button) findViewById(R.id.continuetoprofile);
continuetoprofile.setOnClickListener(this);
}
//SHARED PREFERENCES
/*public void saveInfo(View view){
SharedPreferences save = getSharedPreferences("name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = save.edit();
editor.putString("username",enterusername.getText().toString());
editor.apply();
}*/
public void onClick (View v){
Intent intent = new Intent(this,Profile.class);
intent.putExtra("username", enterusername.getText().toString());
startActivity(intent);
}
public void datePicker(View view){
DatePickerFragment fragment = new DatePickerFragment();
fragment.show(getSupportFragmentManager(), "date");
}
private void setDate(final Calendar calendar) {
final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
((TextView) findViewById(R.id.showDate)).setText(dateFormat.format(calendar.getTime()));
}
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar cal = new GregorianCalendar(year, month, day);
setDate(cal);
}
public static class DatePickerFragment 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);
return new DatePickerDialog(getActivity(),
(DatePickerDialog.OnDateSetListener)
getActivity(), year, month, day);
}
}
Intent intent = getIntent();
public void privPol(View view){
Intent intent = new Intent(this, PrivacyPolicy.class);
startActivity(intent);
}
public void TOU(View view){
Intent intent = new Intent(this, TermsOfUse.class);
startActivity(intent);
}
public void toProfile(View view){
Intent intent = new Intent(this, Profile.class);
startActivity(intent);
}
PROFILE.CLASS
public class Profile extends AppCompatActivity {
//private SharedPreferences spref;
//TextView nameofuser;
TextView nameofuser;
private TextView daystxt, hourstxt, minutestxt, secondstxt;
private Handler handler;
private Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
initUI();
countDownStart();
nameofuser = (TextView)findViewById(R.id.nameofuser);
Intent intent = getIntent();
String username = intent.getStringExtra("username");
nameofuser.setText("Welcome, " + username);
}
@SuppressLint("SimpleDateFormat")
private void initUI() {
daystxt = (TextView) findViewById(R.id.days);
hourstxt = (TextView) findViewById(R.id.hours);
minutestxt = (TextView) findViewById(R.id.minutes);
secondstxt = (TextView) findViewById(R.id.seconds);
}
public void countDownStart() {
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// Here Set your Event Date
Date futureDate = dateFormat.("2016-8-10");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
daystxt.setText("" + String.format("%02d", days));
hourstxt.setText("" + String.format("%02d", hours));
minutestxt.setText("" + String.format("%02d", minutes));
secondstxt.setText("" + String.format("%02d", seconds));
} else {
handler.removeCallbacks(runnable);
// handler.removeMessages(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 0);
}
Intent intent = getIntent();
public void toMenu(View view) {
Intent intent = new Intent(this, Menu.class);
startActivity(intent);
}
public void tostartdate(View view) {
Intent intent = new Intent(this, EditStartdate.class);
startActivity(intent);
}
答案 0 :(得分:1)
//Get the date
Date buttonDate = SimpleDateFormat.getDateInstance().parse(mDate.getText().toString());
和
//Create a bundle to pass the date and send your date as Long
Bundle currentDate = new Bundle();
currentDate.putLong("setDate", buttonDate.getTime());
和
//Read the passed bundle from the next activity get Long and convert it to Date
Bundle setDate = this.getArguments();
Long currDate = setDate.getLong("setDate");
注意: Date构造函数接受的时间长度以毫秒为单位,而不是秒。你需要将它乘以1000,并确保你提供它。
转换
Date d = new Date(1220227200L * 1000);
答案 1 :(得分:0)
尝试使用putExtras()方法将其传递给Bundle中的Intent。
以下代码将有所帮助:
通过捆绑包发送数据
Intent i = new Intent(this, ActivityTwo.class);
Bundle bundle = new Bundle();
bundle.putExtras("Date",YOUR_DATE);
i.putExtras(bundle);
startActivity(i);
在新活动中获取数据
Bundle bundle = getIntent().getExtras();
String date = bundle.getString(“Date”);
这将有助于