我是新手android開發人員。如何從日期選擇器(日曆視圖)更改日期格式
現在,我嘗試將日期格式從m/d/yyyy更改爲mmddyyyy。
這是參考鏈接:http://www.thaicreate.com/mobile/android-calendarview.html
希望能有人幫我。
對不起,我的英語語言不好。
myCalendarView.java
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.widget.CalendarView;
import android.content.Intent;
public class myCalendarView extends Activity {
\t
private int mYear;
private int mMonth;
private int mDay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CalendarView calendar = new CalendarView(this);
calendar.setOnDateChangeListener(mDateSetListener);
setContentView(calendar);
}
// the callback received when the user "sets" the date in the dialog
private CalendarView.OnDateChangeListener mDateSetListener =
new CalendarView.OnDateChangeListener() {
public void onSelectedDayChange(CalendarView view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
String selectedDate = new StringBuilder().append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear).append(" ").toString();
Bundle b = new Bundle();
b.putString("dateSelected", selectedDate);
//Add the set of extended data to the intent and start it
Intent intent = new Intent();
intent.putExtras(b);
setResult(RESULT_OK,intent);
finish();
}
};
}
MainActivity.java
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import java.util.Calendar;
import android.content.Intent;
public class MainActivity extends Activity {
\t
private EditText editT1;
private Button btn1;
private int mYear;
private int mMonth;
private int mDay;
static final int CALENDAR_VIEW_ID = 1;
\t
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// editText1
editT1 = (EditText) findViewById(R.id.editText1);
// button1
btn1 = (Button) findViewById(R.id.button1);
// add a click listener to the button
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, myCalendarView.class);
startActivityForResult(intent,CALENDAR_VIEW_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// display the current date
updateCurrentDate();
\t \t
}
// updates the date we display in the TextView
private void updateCurrentDate() {
\t editT1.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" "));
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode) {
case CALENDAR_VIEW_ID:
if (resultCode == RESULT_OK) {
Bundle bundle = data.getExtras();
editT1.setText(bundle.getString("dateSelected"));
break;
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
請指定你想改變日期格式。我在這裏沒有看到任何「m/d/yyyy」。 –
@Boonya Kitpitak我在日曆上挑選日期後。它會顯示在2017年3月1日的結果中。但我想展示像03/01/2017。 –