저는 Android Studio를 처음 사용하고 있으며 간단한 카운트 다운 타이머 앱을 만들고 있습니다. 나는 2 가지 활동을하고있다. 첫 번째 활동 (로그인) 사용자가 날짜 선택 도구를 통해 날짜를 선택하면 두 번째 활동 (프로필)에 카운트 다운 타이머가 표시됩니다. 카운트 다운 타이머는 자바 클래스에서 날짜를 설정할 때 완벽하게 작동하지만 로그인 활동에서 날짜를 검색하는 데 문제가 있습니다. 두 가지 활동에 대한 내 코드는 아래와 같습니다.datepicker에서 android의 다른 활동으로 날짜를 가져 오는 방법
날짜를 변경해야한다는 것을 알고 있습니다. 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);
}
당신은 문제를 파악하려고 무슨 짓을 한거야? 어떤 값이 반환되는지 확인하려면 코드를 단계별로 실행하려고 시도 했습니까? 예외가 있습니까? –
@ DavidT.Macknet 나는 문자열 이름을 검색하는 데 사용했던 것과 같은 방법으로 인 텐트를 사용해 보았지만 작동하지 않았습니다. – sharms