【问题标题】:How to set different date title in toolbar on every different click on date in calendar?如何在日历中每次不同的点击日期时在工具栏中设置不同的日期标题?
【发布时间】:2021-01-15 19:12:45
【问题描述】:

在此活动中,设置了日历视图。当我单击任何日期时,它会打开一个新活动,但在工具栏中它只显示当前日期。我想显示在日历中单击的日期、月份和年份,每个不同的日期打开相同的活动,但编辑文本应该不同。

public class DiaryFragment extends Fragment {

   

        // Add Listener in calendar
        cale
        return view;
    }
}

   @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()){
                case caledartextpost:
                    Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
                    calendartextview.setCursorVisible(false);
                    break;
            }
            return super.onOptionsItemSelected(item);


    }
}

【问题讨论】:

  • 如果您想将日期从一项活动传递到另一项活动..请改用Intent............
  • 但是如何将数据从片段传递到不同活动的工具栏
  • 添加了答案.......

标签: java android date datepicker calendar


【解决方案1】:

在日历中每次不同的点击日期时,在工具栏中设置不同的日期标题

我已经完成了使用意图

这里有一个演示:--

片段活动:---

public class FragmentActivity extends Fragment {
TextView dateView;
 CalendarView calendar;

public FragmentActivity() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_menu1, container, false);

}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    dateView = view.findViewById(R.id.dateView);
    calendar = view.findViewById(R.id.calender);
    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
            String Date = dayOfMonth + "-" + (month + 1) + "-" + year;

            Intent intent = new Intent(getActivity(), AnotherActivity.class);
            intent.putExtra("name", Date);
            startActivity(intent);
            dateView.setText(Date);
        }
    });

}
 }

AnotherActivity.java:----

public class AnotherActivity extends AppCompatActivity {
TextView textView;
Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.next);
    textView=findViewById(R.id.display);
    toolbar=findViewById(R.id.toolbar);

        Intent intent = getIntent();
        String name = intent.getStringExtra("name");

        textView.setText(name);
        toolbar.setTitle(name);
        setSupportActionBar(toolbar);

}
}

输出:---

点击-->

【讨论】:

  • 它可以工作,但现在 Calendar_date_text.class 中没有显示菜单
  • 你在风格主题中使用什么主题?darkactionbar 或 noactionbar?
  • 我没有使用操作栏。
  • @HarnoorSingh 查看我的编辑...使用 setSupportActionBar(toolbar);显示菜单...我忘记添加了
猜你喜欢
  • 1970-01-01
  • 2019-05-05
  • 1970-01-01
  • 2012-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多