start_image_view.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(TIME_OPENDIALOG_ID);
updateDisplay();
}
});
private TimePickerDialog.OnTimeSetListener mopenSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
mCalendar.set(Calendar.MINUTE, mMinute);
SimpleDateFormat mSDF = new SimpleDateFormat("hh:mm a");
String time = mSDF.format(mCalendar.getTime());
updateDisplay();
}
};
private Object pad(int mMinute2) {
if (mMinute2 >= 10)
return String.valueOf(mMinute2);
else
return "0" + String.valueOf(mMinute2);
}
private void updateDisplay() {
extraTime=mHour+globalFastingHours;
netTimeMinutes=mMinute+globalFastingMinutes;
String timeSet="";
int hours=mHour;
System.out.println("mHour="+mHour);
if(hours>12){
hours -= 12;
timeSet = "PM";
System.out.println("timeSet="+timeSet);
} else if (hours == 0) {
hours += 12;
timeSet = "AM";
System.out.println("timeSet2="+timeSet);
} else if (hours == 12){
timeSet = "PM";
System.out.println("timeSet3="+timeSet);
}
else
timeSet = "AM";
start_image_view.setText(new StringBuilder().append(pad(hours)).append(":")
.append(pad(mMinute)).append((timeSet)));
if(extraTime>=24){
extraTime=extraTime-24;
end_image_view.setText(new StringBuilder().append(pad(extraTime)).append(":")
.append(pad(netTimeMinutes)).append((timeSet)));
}
请帮帮我
答案 0 :(得分:0)
使用DialogFragment和界面作为对活动的回调,并将时间设置为textview。
根据您的要求修改以下内容。
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="180dp"
android:text="TextView" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity implements Returndata {
TextView tv;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void setText(String result) {
// TODO Auto-generated method stub
tv.setText(result);
}
}
TimePickerFragment
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
String AM_PM;
Returndata rd;
public interface Returndata{
void setText(String result);
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
rd= (Returndata) activity;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
Log.i("...............","..........."+hour+"......."+minute);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
if(hourOfDay < 12) {
AM_PM = "AM";
} else {
AM_PM = "PM";
}
StringBuilder sb = new StringBuilder();
sb.append(hourOfDay);
sb.append(":");
sb.append(minute);
sb.append(AM_PM);
rd.setText(sb.toString());
}
}
对齐