gpt4 book ai didi

java - 如何从日期选择器获取日期到android中的另一个 Activity

转载 作者:行者123 更新时间:2023-12-01 09:36:32 27 4
gpt4 key购买 nike

我对 Android Studio 非常陌生,我正在构建一个简单的倒计时器应用程序。我有 2 项 Activity 。第一个 Activity (登录)用户通过日期选择器选择日期,第二个 Activity (个人资料)显示倒计时器。当我在 java 类中为其设置日期时,倒计时器工作得很好,但我在尝试从登录 Activity 中检索日期时遇到了麻烦。我的这两项 Activity 的代码如下。

我知道我必须更改日期 futureDate = dateFormat.parse("2016-8-10");但我不知道怎么做。

登录类

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

}

配置文件.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);
}

最佳答案

    //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);

关于java - 如何从日期选择器获取日期到android中的另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38854221/

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