英文:
How to set different date title in toolbar on every different click on date in calendar?
问题
在这个活动中,设置了日历视图。当我点击任何日期时,它会打开一个新的活动,但在工具栏中只显示当前日期。我希望能够显示在日历中点击的日期、月份和年份,并且每个不同的日期都会打开相同的活动,但编辑框的内容应该不同。
public class DiaryFragment extends Fragment {
// 在日历中添加监听器
cale
return view;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case caledartextpost:
Toast.makeText(this, "已保存", Toast.LENGTH_SHORT).show();
calendartextview.setCursorVisible(false);
break;
}
return super.onOptionsItemSelected(item);
}
}
英文:
In this activity, calendar view is set. When I clicked on any date, it opened a new activity but in toolbar it shows only current date. I want to show that date, month, and year, which clicked in calendar and every different date open same activity but edittext should be different.
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);
}
}
答案1
得分: 1
在每次单击日历上的日期时,在工具栏中设置不同的日期标题
我已经使用意图完成了
以下是演示:--
>> **片段活动:---**
```java
public class FragmentActivity extends Fragment {
TextView dateView;
CalendarView calendar;
public FragmentActivity() {
// 必要的空公共构造函数
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 为此片段填充布局
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);
}
}
输出:---
点击时-->;
<details>
<summary>英文:</summary>
**set different date title in toolbar on every different click on date in calendar**
i have done using intent
## Here's a demo for you :--
>>**Fragment Activity:---**
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);
}
}
## Output:---
[![enter image description here][1]][1]
## Onclick-->
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/bYdFQ.png
[2]: https://i.stack.imgur.com/F42CQ.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论