我正在開發一個應用程序,在此我創建了timepicker,並且我希望將時間選擇器的選定時間縮短6小時(即06:00,如hh:mm)。以下是我使用的代碼。 例如: 如果用戶選擇12:30 pm,那麼它將不得不在文本視圖中顯示06:30 pm。 我該怎麼做?如何從android中的timepicker選擇值獲取減少時間間隔?
//java file
public class ProfileActivity4 extends AppCompatActivity implements View.OnClickListener{
private TextView textView;
private EditText eddate, edtime;
private boolean loggedIn = false;
Button btn;
EditText edname,edaddress;
Button ptime, pdate;
TextView tvsname, tvsprice;
NumberPicker numberPicker;
TextView textview1,textview2;
Integer temp;
String pname, paddress, email, sname, sprice;
private int mYear, mMonth, mDay, mHour, mMinute;
private DrawerLayout drawerLayout;
private Toolbar toolbar;
Spinner spinnerPick;
TextView tdpicktime;
String picktime;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile4);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// back button pressed
Intent i = new Intent(ProfileActivity4.this, List_Activity1.class);
startActivity(i);
}
});
// initNavigationDrawer();
//Initializing textview
textView = (TextView) findViewById(R.id.textView);
edname=(EditText)findViewById(R.id.ed_pname);
edaddress=(EditText)findViewById(R.id.ed_add);
tvsname=(TextView)findViewById(R.id.textView_name);
tvsprice=(TextView)findViewById(R.id.textView2_price);
btn=(Button)findViewById(R.id.button);
spinnerPick=(Spinner)findViewById(R.id.spinner_picktime);
tdpicktime=(TextView)findViewById(R.id.textView_picktime);
// ArrayAdapter<String> sAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.rides));
ArrayAdapter<String> sAdapter1 = new ArrayAdapter<String>(this, R.layout.spinner_item, getResources().getStringArray(R.array.flighttype));
spinnerPick.setAdapter(sAdapter1);
spinnerPick.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
picktime = spinnerPick.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
eddate=(EditText)findViewById(R.id.editText_date);
edtime=(EditText)findViewById(R.id.editText_time);
ptime=(Button)findViewById(R.id.button_ptime);
pdate=(Button)findViewById(R.id.button_pdate);
ptime.setOnClickListener(this);
pdate.setOnClickListener(this);
Intent i = getIntent();
// getting attached intent data
final String name = i.getStringExtra("company_name");
// displaying selected product name
tvsname.setText(name);
String price = i.getStringExtra("sedan_price");
// displaying selected product name
tvsprice.setText(price);
textview1 = (TextView)findViewById(R.id.textView1_amount);
// textview2 = (TextView)findViewById(R.id.textView_seats);
final int foo = Integer.parseInt(price);
textview1.setText(String.valueOf(foo));
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false);
String email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Available");
textView.setText(email);
if (loggedIn) {
submitForm();
Intent intent = new Intent(ProfileActivity4.this, Confirm_Activity4.class);
intent.putExtra("company_name", name);
startActivity(intent);
}
}
});
}
private void submitForm() {
String pname = edname.getText().toString();
String paddress = edaddress.getText().toString();
String sname = textview1.getText().toString();
String time = edtime.getText().toString();
String date = eddate.getText().toString();
String email= textView.getText().toString();
new SignupActivity3(this).execute(pname,paddress,sname,time,date,email);
}
@Override
public void onClick(View v) {
if (v == pdate) {
// Get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
eddate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
if (v == ptime) {
// Get Current Time
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
edtime.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
}
我正在使用24小時制時鐘 – rohiH
calendar.setTime(edtime);在這一行,它顯示錯誤'calender不能應用於java.lang.string' – rohiH
你不能傳遞一個EditText對象,它需要一個Date對象,在** onTimeSet **中構造一個Date對象然後在這個對象上執行你的操作創建Date對象。 –