我的应用程序中有一个 DatePicker,它在 Android 6 之前一直适用于手机和平板电脑。我有一个 Samsung Galaxy 7 Edge t 测试,但没有显示对话框。
我已在 list 中分别将最小和目标 sdk 版本设置为 11 和 21。
我知道我应该使用 DialogFragment 但是下面的代码没有在 Android 6 上显示是有原因的吗?该代码在 2.3.6、4.x 和 5.x 上运行良好。
正在执行以下日志,因此我知道已调用 .show() 方法。
Log.e(TAG, "Just executed dialog.show() and at the end of showDateTimeDialog method");
[编辑] 我尝试过的事情是:
将样式添加到 style.xml 并调用引用该样式的不同 Dialog 构造函数。这确实在 Android 6 上显示了一个对话框,但不幸的是,它不是我使用带有按钮的布局的自定义对话框。
<style name="MyDialogStyle" parent="Theme.AppCompat.Light">
.
dialog = new Dialog(MenuActivity2.this, R.style.MyDialogStyle);
dialog.setContentView(R.layout.datetimepickerunalloc);
谢谢
Button Set;
Button ReSet;
Button Cancel;
DatePicker DPic;
TextView Date;
private ViewSwitcher switcher;
static final int DATE_TIME_DIALOG_ID = 999;
Dialog dialog;
TextView dialogMessage;
DateTime timeSetOnSpinner;
public void showDateTimeDialog() {
// final Calendar c = Calendar.getInstance();
final SimpleDateFormat dfDate = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
dialog = new Dialog(MenuActivity2.this);
dialog.setContentView(R.layout.datetimepickerunalloc);
DPic = (DatePicker) dialog.findViewById(R.id.DatePicker);
Set = ((Button) dialog.findViewById(R.id.SetDateTime));
Set.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
c.set(DPic.getYear(), DPic.getMonth(), DPic.getDayOfMonth());
timeSetOnSpinner = new DateTime(c);
DateTimeFormatter fmt = DateTimeFormat.forPattern("d-MMM-Y");
String formattedDate = fmt.print(timeSetOnSpinner);
formattedDate = formattedDate.trim();
//Toast.makeText(MenuActivity2.this, "date = " + formattedDate, Toast.LENGTH_LONG).show();
final Intent intent = new Intent(MenuActivity2.this, ShowUnallocatedCallsActivity.class);
intent.putExtra("unallocdate", formattedDate);
startActivity(intent);
}// end of onClick
});
ReSet = ((Button) dialog.findViewById(R.id.ResetDateTime));
ReSet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Calendar c = Calendar.getInstance();
DPic.updateDate(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
}
});
dialog.setTitle("Unallocated calls date");
try {
ReSet.performClick();
dialog.show();
Log.e(TAG, "Just executed dialog.show() and at the end of showDateTimeDialog method");
} catch (Exception e) {
// ignore
}
}// showDateTimeDialog()
.
这是自定义对话框的布局
<?xml version="1.0" encoding="UTF-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="700dp"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textviewdatetimepickermessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<LinearLayout
android:id="@+id/DateLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<DatePicker
android:id="@+id/DatePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip" />
</LinearLayout>
<LinearLayout
android:id="@+id/ControlButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip" >
<Button
android:id="@+id/SetDateTime"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/ok" />
<Button
android:id="@+id/ResetDateTime"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset" />
<!--
<Button
android:layout_height="wrap_content"
android:layout_width="0dip"
android:id="@+id/CancelDialog"
android:text="@android:string/cancel"
android:layout_weight="1"/>
-->
</LinearLayout>
</LinearLayout>
</ScrollView>
.
最佳答案
我最终通过使用传入主题 arg 的不同构造函数显示了对话框。
dialog = new Dialog(MenuActivity2.this, R.style.AppBaseTheme );
关于Android DatePicker 对话框未在 Android 6 上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36739731/