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

huangapple go评论69阅读模式
英文:

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&#39;s a demo for you :--

&gt;&gt;**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 + &quot;-&quot; + (month + 1) + &quot;-&quot; + year;

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

    }
     }




&gt;&gt;**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(&quot;name&quot;);

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

    }
    }



## Output:---

[![enter image description here][1]][1]



## Onclick--&gt;


[![enter image description here][2]][2]


  [1]: https://i.stack.imgur.com/bYdFQ.png
  [2]: https://i.stack.imgur.com/F42CQ.png

</details>



huangapple
  • 本文由 发表于 2020年9月30日 14:33:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64132070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定