我正在关注这个库以获取日期时间选择器https://github.com/florent37/SingleDateAndTimePicker它的工作正常但是如果我打开SingleDateAndTimePicker对话框并触发onBack按下方法然后对话框关闭但是我无法返回私有的活动,如果对话框是没有打开然后onBack新闻代码是火上任何解决方案,将不胜感激。 我的代码:
@Override
public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if (singleDateAndTimePickerDialog!=null) {
singleDateAndTimePickerDialog.dismiss();
} else if (currentTime - lastPress > 5000) {
Toast.makeText(getBaseContext(), "Press back again", Toast.LENGTH_LONG).show();
lastPress = currentTime;
} else {
super.onBackPressed();
}
}
答案 0 :(得分:1)
你可以试试这个:
//一个全局变量 private SingleDateAndTimePickerDialog singleDateAndTimePickerDialog = null;
onCreate(){
singleDateAndTimePickerDialog = new SingleDateAndTimePickerDialog(this);
}
@Override
public void onBackPressed() {
if(singleDateAndTimePickerDialog != null
&& singleDateAndTimePickerDialog.isDisplaying()){
singleDateAndTimePickerDialog.dismiss();
return;
}//if
super.onBackPressed();
finish();
}
================================== EDIT ============ =========================
您可以使用Android DatePicker和TimePicker。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="match_parent">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="match_parent"
android:calendarViewShown="true"
android:spinnersShown="false"
android:layout_weight="4"
android:layout_height="0dp" />
<TimePicker
android:id="@+id/time_picker"
android:layout_weight="4"
android:layout_width="match_parent"
android:layout_height="0dp" />
<Button
android:id="@+id/date_time_set"
android:layout_weight="1"
android:layout_width="match_parent"
android:text="Set"
android:layout_height="0dp" />
</LinearLayout>
..和android对话框。
final View dialogView = View.inflate(activity, R.layout.date_time_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(activity).create();
dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
datePicker.getMonth(),
datePicker.getDayOfMonth(),
timePicker.getCurrentHour(),
timePicker.getCurrentMinute());
time = calendar.getTimeInMillis();
alertDialog.dismiss();
}});
alertDialog.setView(dialogView);
alertDialog.show();
所以你可以这样做:
@Override
public void onBackPressed() {
if(alertDialog != null && alertDialog.isShowing()){
alertDialog.dismiss();
return;
}//if
super.onBackPressed();
finish();
}
答案 1 :(得分:0)
一旦对话被解雇,您可以尝试完成活动。
@RunWith(Parameterized.class)
public class SomeTest {
private final String PARAM_NAME = "param-1";
public SomeTest(String someParam){
org.springframework.test.util.ReflectionTestUtils.setField(this, "PARAM_NAME", someParam);
}
@Parameterized.Parameters
public static Collection parameters() {
return Arrays.asList(new Object[][] {
{ "param-1" },
{ "param-2" },
{ "param-3" }
});
}
@Test
@ExternalAnnotation(PARAM_NAME) //needs constant in annotation
public void testSomething() throws Exception {
System.out.println("Testing with: "+PARAM_NAME); //always prints param-1
//some tests
}
}
答案 2 :(得分:0)
试试这个
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
}
答案 3 :(得分:0)
super.onBackPressed();
在其他条件下它对你的状况没问题,因为在这种情况下onback不会因为super.onback处于不正常状态而开火。