我们可以在单个应用程序中使用 onCreateDialog 中的不同整数同时打印时间选择器和日期吗?
Posted
技术标签:
【中文标题】我们可以在单个应用程序中使用 onCreateDialog 中的不同整数同时打印时间选择器和日期吗?【英文标题】:Can we print Time Picker and Date together in single app using differrent integer in onCreateDialog? 【发布时间】:2020-08-18 14:03:54 【问题描述】:包 com.example.timepickerdialog;
导入androidx.appcompat.app.AppCompatActivity;
导入android.app.DatePickerDialog;
导入android.app.Dialog;
导入android.app.TimePickerDialog;
导入android.os.Bundle;
导入android.view.View;
导入android.widget.Button;
导入 android.widget.DatePicker;
导入android.widget.TimePicker;
导入android.widget.Toast;
public class MainActivity extends AppCompatActivity
Button button, button2;
static final int DIALOG_ID = 0, SECONDDIALOGID = 0;
int ApnaHour;
int ApnaMinute, ApnaYear, ApanaDay, ApnaMonth;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DialogListener();
public void DialogListener()
button = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showDialog(DIALOG_ID);
);
button2.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
showDialog(SECONDDIALOGID);
);
此覆盖显示错误中的以下问题。如果我删除 int hh,则 DateDialogPicker 中会显示错误。 请告诉我这段代码是否错误。
@Override
protected Dialog onCreateDialog(int id , int hh)
if (id == DIALOG_ID)
return new TimePickerDialog(MainActivity.this, SaadaTimePickerListener, ApnaHour, ApnaMinute, false);
return null;
if (hh== SECONDDIALOGID)
return new DatePickerDialog(this, DateSaadaPickerListener,ApanaDay, ApnaMonth, ApnaYear);
return null;
请解决上述覆盖错误。
protected TimePickerDialog.OnTimeSetListener SaadaTimePickerListener = new TimePickerDialog.OnTimeSetListener()
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute)
ApnaHour = hourOfDay;
ApnaMinute = minute;
Toast.makeText(MainActivity.this, ApnaHour + ":" + ApnaMinute, Toast.LENGTH_SHORT).show();
;
private DatePickerDialog.OnDateSetListener DateSaadaPickerListener = new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
ApnaMonth = month;
ApnaYear = year;
ApanaDay = dayOfMonth;
Toast.makeText(MainActivity.this, ApanaDay + "/" + ApnaMonth + "/" + ApnaYear, Toast.LENGTH_SHORT).show();
;
【问题讨论】:
【参考方案1】:showDialog 现在已弃用。我已经用当前时间和日期编写了 DatePicker 和 TimePicker 的代码。随意修改。
public void showDatePickerDialog(View v)
// Get Current Date or use your own Date
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int 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)
//get date here
, mYear, mMonth, mDay);
datePickerDialog.show();
public void showTimePickerDialog(View v)
// Get Current Time or use your own Time
final Calendar c = Calendar.getInstance();
int mHour = c.get(Calendar.HOUR_OF_DAY);
int 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)
//get time here
, mHour, mMinute, false);
timePickerDialog.show();
【讨论】:
以上是关于我们可以在单个应用程序中使用 onCreateDialog 中的不同整数同时打印时间选择器和日期吗?的主要内容,如果未能解决你的问题,请参考以下文章
我们可以在AppDelegate中实例化核心数据的单个对象并在整个应用程序中使用它吗?
我们可以在 AWS Cognito 的单个用户池中拥有多个角色吗?
是否可以在单个应用程序中拥有 SAML 2.0 身份验证和 JWT 身份验证?