我希望在一个对话框中有一个日期和两个时间选择器,我已经完成了,但第二次(Time to)得到的值与(time from)相同。我已经设置了一些'if cases'来尝试解决它但没有帮助。我得到(“2016/10/27 5:41 0:00”)即使我选择两个“timepickers”的时间我没有得到第二个的值,或者它重复第一个。
这是我的代码:
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.Calendar;
public class AdminActivity extends AppCompatActivity implements View.OnClickListener, TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
//firebase auth object
private FirebaseAuth firebaseAuth;
//view objects
private TextView textViewUserEmailadmin;
private Button buttonLogout;
private DatabaseReference databaseReference;
private EditText editTextinfo;
private TextView Textpass;
private Button buttonSave;
int day, month, year, hour, minute,hour1,minute1;
int dayFinal, monthFinal, yearFinal, hourFinal, minuteFinal,hourFinal1,minuteFinal1;
int i = 0;
Button btnDatePicker;
String a="";
String b="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
//initializing firebase authentication object
firebaseAuth = FirebaseAuth.getInstance();
//if the user is not logged in
//that means current user will return null
if (firebaseAuth.getCurrentUser() == null) {
//closing this activity
finish();
//starting login activity
startActivity(new Intent(this, LoginActivity.class));
}
databaseReference = FirebaseDatabase.getInstance().getReference();
editTextinfo = (EditText) findViewById(R.id.editText3);
buttonSave = (Button) findViewById(R.id.buttonSave);
//getting current user
FirebaseUser user = firebaseAuth.getCurrentUser();
//initializing views
textViewUserEmailadmin = (TextView) findViewById(R.id.textViewUserEmailAdmin);
buttonLogout = (Button) findViewById(R.id.buttonLogout);
//displaying logged in user name
textViewUserEmailadmin.setText("Welcome admin " + user.getEmail());
//adding listener to button
buttonLogout.setOnClickListener(this);
buttonSave.setOnClickListener(this);
btnDatePicker = (Button) findViewById(R.id.btn_date);
// btnTimeFrom = (Button) findViewById(R.id.btn_time_from);
// btnTimeTo = (Button) findViewById(R.id.btn_time_to);
Textpass = (TextView) findViewById(R.id.Textviewpass);
btnDatePicker.setOnClickListener(this);
}
private void saveUserInformation() {
String info = editTextinfo.getText().toString().trim();
String pass = Textpass.getText().toString().trim();
i++;
UserInformation UserInformation = new UserInformation(info, pass);
// FirebaseUser user = firebaseAuth.getCurrentUser();
//databaseReference.child(user.getUid()+i++).setValue(UserInformation);
databaseReference.child("tidnr" + i).setValue(UserInformation);
Toast.makeText(this, "Information saved...", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View view) {
//if logout is pressed
if (view == buttonLogout) {
//logging out the user
firebaseAuth.signOut();
//closing activity
finish();
//starting login activity
startActivity(new Intent(this, LoginActivity.class));
}
if (view == buttonSave) {
saveUserInformation();
}
if (view == btnDatePicker) {
if(a == "" && b == "") {
Calendar c = Calendar.getInstance();
day = c.get(Calendar.DAY_OF_MONTH);
month = c.get(Calendar.MONTH);
year = c.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(AdminActivity.this, AdminActivity.this, year, month, day);
datePickerDialog.show();
}
}
}
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
yearFinal=i;
monthFinal=i1 +1;
dayFinal= i2;
Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
if(a == "" && b == "") {
android.text.format.DateFormat dateFormat = new android.text.format.DateFormat();
//is24HourFormat = dateFormat.is24HourFormat(this);
TimePickerDialog timePickerDialog = new TimePickerDialog(AdminActivity.this, AdminActivity.this,
hour, minute, dateFormat.is24HourFormat(this));
timePickerDialog.show();
}
}
@Override
public void onTimeSet(TimePicker view, int i, int i1) {
hourFinal = i;
minuteFinal = i1;
String curTime = String.format("%02d",minuteFinal);
if(a == "" && b == ""){
a = yearFinal+"/"+monthFinal+"/"+dayFinal+" "+hourFinal+":"+curTime;
}
if(a != "" && b == "" ) {
Calendar c1 = Calendar.getInstance();
hour1 = c1.get(Calendar.HOUR_OF_DAY);
minute1 = c1.get(Calendar.MINUTE);
android.text.format.DateFormat dateFormat1 = new android.text.format.DateFormat();
//is24HourFormat = dateFormat.is24HourFormat(this);
TimePickerDialog timePickerDialog1 = new TimePickerDialog(AdminActivity.this, AdminActivity.this,
hour1, minute1, dateFormat1.is24HourFormat(this));
timePickerDialog1.show();
String curTime1 = String.format("%02d",minuteFinal1);
b = a+" "+hourFinal1+":"+curTime1;
a="";
Textpass.setText(b);
Toast.makeText(this, "test.", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:1)
替换此行
String curTime1 = String.format("%02d",minuteFinal1);
b = a+" "+hourFinal1+":"+curTime1;
使用:
String curTime1 = String.format("%02d",minute1);
b = a+" "+hour1+":"+curTime1;