大家好,我想知道,我怎样才能通过按钮点击日期传递给一个caldroid日历片段?所以我可以编辑日期颜色使点击等。
Caldroid Fragment:
public class Caldroid_fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.caldroid_calendar, container, false);
return rootView;
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
String dt = Testes.notas.get("Teste");
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date teste_portugues = null;
try {
teste_portugues = sdf.parse(dt);
} catch (ParseException e) {
e.printStackTrace();
}
ColorDrawable green = new ColorDrawable(Color.GREEN);
CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
args.putInt(CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY);
caldroidFragment.setArguments(args);
Calendar cal = Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
caldroidFragment.setBackgroundDrawableForDate(green,teste_portugues);
caldroidFragment.setArguments(args);
FragmentTransaction t = getChildFragmentManager().beginTransaction();
t.replace(R.id.caldroidCal, caldroidFragment);
t.commit();
}}
两个片段的父类:
public class Testes extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private DatePickerDialog.OnDateSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_picker;
private TextView date_text;
private TextView hora_text;
public static Bundle notas = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_testes, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case go_to_fazer_media:{
if (item.isChecked())
item.setChecked(false);
else{
Intent i = new Intent(Testes.this,Formas.class);
startActivity(i);
return true;
}
}
case definicoes:{
if (item.isChecked())
item.setChecked(false);
else{
return true;
}
}
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
Tab1tests tab1 = new Tab1tests();
return tab1;
case 1:
Tab2calendar tab2 = new Tab2calendar();
return tab2;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Adicionar Testes";
case 1:
return "Mapa de Testes";
}
return null;
}
}
}
我试图传递信息的片段:
public class Tab1tests extends Fragment {
private ViewPager mViewPager;
private TimePickerDialog.OnTimeSetListener hourSetListener;
private DatePickerDialog.OnDateSetListener dateSetListener;
private Button date_button;
private Button hora_button;
private TextView date_text;
private TextView hora_text;
public static Button add_test;
private String dt;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.tab1tests, container, false);
add_test = (Button) rootView.findViewById(R.id.add_test);
date_button = (Button) rootView.findViewById(R.id.date_button);
hora_text = (TextView) rootView.findViewById(R.id.hora_text);
hora_button = (Button) rootView.findViewById(R.id.hora_button);
date_text = (TextView) rootView.findViewById(R.id.date_text);
date_button.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog date_dialog = new DatePickerDialog(
getActivity(),
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
dateSetListener,
year, month, day);
date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
date_dialog.show();
}
;
}
);
dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
date_button.setVisibility(View.GONE);
date_text = (TextView) rootView.findViewById(R.id.date_text);
date_text.setVisibility(View.VISIBLE);
dt = day + "-" + month + "-" + year;
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
try {
sdf.parse(dt);
date_text.setText(dt);
} catch (ParseException e) {
e.printStackTrace();
}
}
};
hora_button = (Button) rootView.findViewById(R.id.hora_button);
hora_button.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int hora = cal.get(Calendar.HOUR);
int minutos = cal.get(Calendar.MINUTE);
TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(), TimePickerDialog.THEME_HOLO_LIGHT, hourSetListener, hora, minutos, true);
mTimePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mTimePicker.show();
}
}
);
hourSetListener = new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int hora, int minutos) {
hora_button.setVisibility(View.INVISIBLE);
hora_text.setVisibility(View.VISIBLE);
hora_text.setText(hora + ":" + minutos);
}
};
hora_text.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int hora = cal.get(Calendar.HOUR);
int minutos = cal.get(Calendar.MINUTE);
TimePickerDialog mTimePicker = new TimePickerDialog(getActivity(),TimePickerDialog.THEME_HOLO_LIGHT,hourSetListener,hora,minutos,true);
mTimePicker.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
mTimePicker.show();
}
});
date_text.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog date_dialog = new DatePickerDialog(
getActivity(),
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
dateSetListener,
year, month, day);
date_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
date_dialog.show();
}
});
add_test.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Testes.notas.putString("Teste"+ contador3,dt);
}
});
return rootView;
}
}