当我的应用程序中的菜单项打开datePicker并选择日期时,textview' selectedDateView'只有当我再次打开选项菜单时,我的UI才会更新。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
DialogFragment newFragment = new SettingsDatePicker();
newFragment.show(getFragmentManager(), "Select the date");
// i think this is called only the second time
selectedDateView.setText(String.format("Selected Date: %s", settings.getConfiguredDateTime().toString(fmt)));
return true;
}
return super.onOptionsItemSelected(item);
}
有没有办法覆盖OnSettingMenuItemFinished或类似的东西?
感谢您的帮助。
答案 0 :(得分:0)
使用马里奥在评论中告诉我的建议解决了这个问题。
**EDIT: Add of Source**
// Activity which calls the datepicker. I KNOW THAT THE CODE CAN BE IMPROVED! Just for trials.
public class MyActivity extends Activity
{
/*
* Code blabla
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
DialogFragment newFragment = new SettingsDatePicker();
newFragment.setCallback(this);
newFragment.show(getFragmentManager(), "Select the date");
return true;
}
return super.onOptionsItemSelected(item);
}
public void CallBack(String text)
{
// i think this is called only the second time
selectedDateView.setText(String.format("Selected Date: %s", text));
}
/*
* Code blabla
*/
}
// DialogFragment
public class SettingsDatePicker extends DialogFragment
{
private MyActivity activity = null;
/*
* Code blabla
*/
public void setCallback(MyActivity activity) {
//set callback class
this.activity = activity;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
this.activity.CallBack("TextToUpdate");
}
/*
* Code blabla
*/
}