gpt4 book ai didi

android - 启动新 Activity 的日历日期选择

转载 作者:行者123 更新时间:2023-11-30 01:51:23 26 4
gpt4 key购买 nike

主要 Activity 包含带有按钮的操作栏,在同一屏幕上显示日历。
我想在日历 View 中选择日期时启动新 Activity 。
为此,我编写了这样的代码。

public class Main extends Activity {
CalendarView cal;
String d,m,y;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout llAppointment, llAdmitPatient, llAdmitDischarge, lle_counsults, llSecondOpinion, llQandA;
cal = (CalendarView) findViewById(R.id.calndrView);
llAppointment = (LinearLayout) findViewById(R.id.LLAppointment);
llAdmitPatient = (LinearLayout) findViewById(R.id.LLAdmitPatient);
llAdmitDischarge = (LinearLayout) findViewById(R.id.LLAdmitDischarge);
lle_counsults = (LinearLayout) findViewById(R.id.LLE_consults);
llSecondOpinion=(LinearLayout) findViewById(R.id.LLSecondOpinions);
llQandA = (LinearLayout) findViewById(R.id.LLQandA);


//appointments
llAppointment.setOnClickListener(new OnClickListener() {

@SuppressLint("SimpleDateFormat")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
String Date = df.format(c.getTime());


String day=Date.substring(0,2);
String month=Date.substring(3,5);
String year=Date.substring(8,10);

d=day;m=month;y=year;

Intent intent = new Intent(Main.this, AllPatientsActivity.class);
intent.putExtra("day", day);
intent.putExtra("month", month);
intent.putExtra("year", year);

startActivity(intent);


}
});

// Admitted patients

llAdmitPatient.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent= new Intent(Main.this, AdmittedPatients.class);
startActivity(intent);
}
});
// Admit-Discharge
llAdmitDischarge.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Main.this, AdmitDischarge.class);
startActivity(intent);
}
});
//E-consultant
lle_counsults.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Main.this, EConsultant.class);
startActivity(intent);
}
});
//Second Opinion
llSecondOpinion.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Main.this, SecondOpinion.class);
startActivity(intent);
}
});
//Question and answer
llQandA.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Main.this, QAndA.class);
startActivity(intent);
}
});

cal.setOnDateChangeListener(new OnDateChangeListener() {

@Override
public void onSelectedDayChange(CalendarView view, int year,
int month, int day) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),
day + "/" + month + "/" + year, Toast.LENGTH_SHORT)
.show();



if(Integer.parseInt(d)==day && Integer.parseInt(m)==month && Integer.parseInt(y)==year){
Toast.makeText(Main.this, "day:"+day+"d"+d+"month:"+month+"m"+m+"year:"+year+"y"+y, Toast.LENGTH_LONG).show();
}


Intent intent = new Intent(Main.this, AllPatientsActivity.class);
intent.putExtra("day", day);
intent.putExtra("month", month);
intent.putExtra("year", year);
startActivity(intent);
}

});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);

return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_calendar:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
cal.setVisibility(View.VISIBLE);

return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

当我按下操作栏按钮时,它应该启动 calendarView,如果我在该日历 View 中选择了一个日期,它应该启动显示该日期的第二个 Activity 。但问题是此代码不允许 mi 选择本月以外的日期,因为一旦本月在滚动时发生变化,它就会触发事件。
我应该在代码中做哪些更改,以便我的新 Activity 将以正确的日期启动

最佳答案

您可以使用 Intent.putExtra(String date)用于将日期从一项 Activity 传递到另一项 Activity

在主要 Activity 中

//Create new Intent Object, and specify class
Intent intent = new Intent();

intent.setClass(Main.this,AllPatientsActivity.class);

intent.putExtra("passdate","Date_var_here");

Main.this.startActivity(intent);

在所有患者 Activity 中
//obtain  Intent Object send  from SenderActivity
Intent intent = this.getIntent();

/* Obtain String from Intent */
if(intent !=null)
{
String strdata = intent.getExtras().getString("passdate");
// DO SOMETHING HERE
}
else
{
// DO SOMETHING HERE
}

关于android - 启动新 Activity 的日历日期选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32989043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com