当我选择12:00或i选择0:00它只显示我AM不PM尝试了很多但没有得到所需的输出我在堆栈溢出中搜索相同的问题但没有得到输出..
这里的完整代码如下: -
main.java
package com.Weal.sachin.omcom;
import android.content.DialogInterface;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import java.util.Calendar;
import java.util.Locale;
import android.view.ViewGroup;
import android.widget.EditText;
import android.app.TimePickerDialog;
import android.widget.TimePicker;
public class Daily_Task extends Fragment {
public Daily_Task(){};
boolean isFromClicked = false;
boolean morning = true;
private int hour;
private int minute;
String addtask, adddetail, addstart, addend;
static final int TIME_DIALOG_ID = 999;
View view;
EditText starttime;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_daily__task, container, false);
getActivity().setTitle("Task");
starttime = (EditText) view.findViewById(R.id.start_time);
setCurrentTimeOnView();
addListenerOnButton();
return view;
}
// display current time
public void setCurrentTimeOnView() {
// starttime= (EditText)view.findViewById(R.id.start_time);
// timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
final Calendar c = Calendar.getInstance();
hour = c.get(Calendar.HOUR);
minute = c.get(Calendar.MINUTE);
// set current time into textview
starttime.setText(getTimeText(hour, minute));
// set current time into timepicker
//timePicker1.setCurrentHour(hour);
//timePicker1.setCurrentMinute(minute);
}
public void addListenerOnButton() {
// starttime = (EditText) view.findViewById(R.id.start_time);
starttime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showTimePickerDialog();
isFromClicked = false;
}
});
}
private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int selectedHour,
int selectedMinute) {
hour = selectedHour;
minute = selectedMinute;
// set current time into textview
if (isFromClicked)
starttime.setText(getTimeText(hour, minute));
else{
}
//btnChangeTime.setText(getTimeText(hour, minute));
// set current time into timepicker
// timePicker1.setCurrentHour(hour);
// timePicker1.setCurrentMinute(minute);
}
};
private void showTimePickerDialog() {
new TimePickerDialog(getContext(), timePickerListener, hour, minute, true).show();
}
private String getTimeText(int hourOfDay, int minute) {
int hour = hourOfDay % 12;
if (hour == 0) hour = 12 ;
//then
if (hour >=12 && hour < 24) morning = false;
else morning = true;
String _AM_PM = (hourOfDay > 12) ? "PM" : "AM";
return String.format(Locale.getDefault(), "%02d:%02d %s", hour, minute, _AM_PM);
}
}
答案 0 :(得分:0)
我尝试了这段代码,现在它对我来说非常有用: -
<强> TimePicker.java 强>
package com.Weal.sachin.omcom;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.icu.text.SimpleDateFormat;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TimePicker;
import java.util.Calendar;
import java.util.Date;
/**
* Created by sachin on 2/2/2017.
*/
public class TImePickerEndTime extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
// Get a Calendar instance
final Calendar calendar = Calendar.getInstance();
// Get the current hour and minute
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
// TimePickerDialog Theme : THEME_DEVICE_DEFAULT_LIGHT
TimePickerDialog tpd = new TimePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,hour,minute,false);
// TimePickerDialog Theme : THEME_DEVICE_DEFAULT_DARK
TimePickerDialog tpd2 = new TimePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,hour,minute,false);
// TimePickerDialog Theme : THEME_HOLO_DARK
TimePickerDialog tpd3 = new TimePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_DARK,this,hour,minute,false);
// TimePickerDialog Theme : THEME_HOLO_LIGHT
TimePickerDialog tpd4 = new TimePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT,this,hour,minute,false);
// TimePickerDialog Theme : THEME_TRADITIONAL
TimePickerDialog tpd5 = new TimePickerDialog(getActivity(),
AlertDialog.THEME_TRADITIONAL,this,hour,minute,false);
// Return the TimePickerDialog
return tpd;
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute){
String am_pm = "";
String tm = new SimpleDateFormat("HH:mm").format(new Date(hourOfDay));
Calendar datetime1 = Calendar.getInstance();
datetime1.set(Calendar.HOUR_OF_DAY, hourOfDay);
datetime1.set(Calendar.MINUTE, minute);
String strDate = tm.format(String.valueOf(datetime1.getTime()));
if (datetime1.get(Calendar.AM_PM) == Calendar.AM)
am_pm = "AM";
else if (datetime1.get(Calendar.AM_PM) == Calendar.PM)
am_pm = "PM";
String strHrsToShow1 = (datetime1.get(Calendar.HOUR) == 00)?"12":datetime1.get(Calendar.HOUR)+"";
// ((EditText)getActivity().findViewById(R.id.End_time)).setText(String.format("%02d:%02d", strHrsToShow1, datetime1.get(Calendar.MINUTE) + "") +" "+am_pm);
// ((EditText)getActivity().findViewById(R.id.End_time)).setText(String.format("%02d:%02d", strHrsToShow1, datetime1.get(Calendar.MINUTE)));
/*((EditText)getActivity().findViewById(R.id.End_time)
).setText( String.format("%02d:%02d", strHrsToShow1, datetime1.get(Calendar.MINUTE) + "") +" "+am_pm);
*/
int hour = hourOfDay % 12;
((EditText) getActivity().findViewById(R.id.End_time)).setText(String.format("%02d:%02d %s", hour == 0 ? 12 : hour,
minute, hourOfDay < 12 ? "am" : "pm"));
}
}
现在位于 Main.java
//
timepicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Initialize a new time picker dialog fragment
DialogFragment dFragment1 = new TImePicker();
// Show the time picker dialog fragment
dFragment1.show(getActivity().getFragmentManager(),"Time Picker");
}
});