主要活动包含带有按钮的操作栏,该按钮在同一屏幕上显示日历。 我想在日历视图中选择日期时启动新活动。 为此,我写了这样的代码。
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,如果我在该日历视图中选择了一个日期,它应该启动显示该日期的第二个活动。但问题是这段代码不允许mi选择本月以外的日期,因为一旦本月更改,滚动它就会触发事件。 我应该在代码中做些什么更改,以便我的新活动将以正确的日期启动
答案 0 :(得分:1)
您可以使用<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/exit_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top|left"
android:orientation="vertical">
<ImageButton
android:id="@+id/exit_button_mode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="@drawable/Capture" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:text="Main Menu"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/modes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose Game Mode"
android:textSize="30sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#aaaaaa"
android:gravity="center"
android:orientation="vertical">
<!--remove bg color from linear layout-->
<!--add your buttons in place of textview-->
<TextView
android:layout_width="300dp"
android:layout_height="300dp"
android:gravity="center"
android:text="Add your remaining layout here"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
将日期从一个活动传递到另一个活动
在主要活动中
Intent.putExtra(String date)
在AllPatientsActivity活动中
//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);