我之前提出的问题get the value from a method in another class可以理解,因为它的重复,但我仍然无法找到解决问题的方法。如果有人帮忙,我将不胜感激。我进步了一点,但这还不够。 首先,我目前的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity
implements CalFragment.OnDateSelected{
private TextView mTextMessage;
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.menu_home);
return true;
case R.id.navigation_dashboard:
//mTextMessage.setText(date);
showDateDialog();
mTextMessage.setText(date);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.menu_location);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
public void showDateDialog() {
DialogFragment CalFragment = new CalFragment();
CalFragment.show(getSupportFragmentManager(), "timePicker");
//date = "Hello";
}
}
和
CalFragment.java
public class CalFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
OnDateSelected mcallback;
public interface OnDateSelected {
public void NewStrDate(String strDate);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
Date d = new Date(year, month, day);
String strDate = dateFormatter.format(d);
}
}
我想提供从CalFragment到ShowDate对话的日期,但它不起作用。在目前的状态下,它显示implements
中的MainActivity
声明是错误的。我正在阅读一些java教程,但它只是我的第二天。所以请多多帮助。