Android How to get each day of month and display it within EditText by a Button event

Android How to get each day of month and display it within EditText by a Button event

I was wondering how to display each day of month and list them out within an EditText. After my research, I figured it out by use a silly method. But at least it works fine and easy to understand! So the following codes are implementations of it.


Part 1 – We have to declare an EditText and a Button by use this snippet code(Don’t forget to initialize the xml layout):


Part 2 – Then, we need to set OnClickListener to buttonAddTime in terms of use OnClick event:


Part 3 – Declare an int as “cursorPosition” in order to put user’s cursor at start position.

Declare a variable named “sdf” to get date format by use system function SimpleDateFormat.

Declare a variable named “month” as String and assign our date format “sdf” to it.

Declare a variable named “date” and initial it to “null”, we would use this to display the final result of date format that we created.

Declare a variable named “c” and make it as a instance of Calendar function.

Declare a variable named “monthMaxDays”, to get number of days of month by use getActualMaximum function.


Part 4 – This is how I wrote code to display month and day and display it in an EditText. Kind of silly… Just use “monthMaxDays” to compare if it is 28~31 days of a month, then do print out the date. As you can see the last line of this code mBodyText.getText().insert(cursorPosition, date); is use insert function to add information into EditText.


All in all, the completed buttonAddTime should look like this(For result, you guys can refer to the image I put at the beginning):


That’s all! If you have any better solutions about how to get each day of month and display it please share with me~


</catmee> 文章全部原创、谢绝转载,作者「BianLei」,如果喜欢欢迎分享链接,相关内容参见本站版权声明
本站相关:QQ空间 | Facebook
Tagged . Bookmark the permalink.

Comment Anonymously? No Problem! | 可以匿名评论了我会说?

Fill up Email address to subscribe reply and display your avatar from Gravatar (If any)