当我从datepicker中选择一个日期时,翻转位置。我想要mm / dd / yyyy。相反,我得到dd / mm / yyyy。
我尝试了本网站上描述的一些方法来修复它,但它没有帮助。例如HOUR_OF_DAY,而不是HOUR。但是,它似乎不起作用。
有任何建议,建议吗?
提前致谢:)
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import sync.DatabaseHandler;
import sync.UserFunctions;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
public class Register extends Activity {
/**
* JSON Response node names.
**/
private static String KEY_SUCCESS = "success";
private static String KEY_UID = "uid";
private static String KEY_FIRSTNAME = "fname";
private static String KEY_LASTNAME = "lname";
private static String KEY_DOB = "dob";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
private static String KEY_ERROR = "error";
/**
* Defining layout items.
**/
EditText inputFirstName;
EditText inputLastName;
EditText inputEmail;
EditText inputPassword;
Button btnRegister;
TextView registerErrorMsg;
//Datpicker setup
Button dateButton;
static final int DATE_DIALOG_ID = 999;
private DatePicker dobPicker;
TextView dobResultView;
private int year;
private int month;
private int day;
String dob;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
inputFirstName = (EditText) findViewById(R.id.firstNameInput);
inputLastName = (EditText) findViewById(R.id.lastNameInput);
inputEmail = (EditText) findViewById(R.id.emailInput);
inputPassword = (EditText) findViewById(R.id.passwordInput);
btnRegister = (Button) findViewById(R.id.registerBtn);
registerErrorMsg = (TextView) findViewById(R.id.register_error);
dobPicker = (DatePicker)findViewById(R.id.dobSelect);
dateButton = (Button)findViewById(R.id.dateBtn);
dobResultView = (TextView)findViewById(R.id.dobResultView);
setCurrentDateOnView();
/**
* Return to login screen
**/
Button login = (Button) findViewById(R.id.loginBtn);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Login.class);
startActivityForResult(myIntent, 0);
finish();
}
});
/**
* Register Button click event.
* A Toast on fields if empty.
**/
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog(DATE_DIALOG_ID);
dob = " ("+Integer.toString(day)+"/"+Integer.toString(month+1)+"/"+Integer.toString(year)+")";
dobResultView.setText(dob);
}
});
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if ( ( !inputPassword.getText().toString().equals("")) && ( !inputFirstName.getText().toString().equals("")) && ( !inputLastName.getText().toString().equals("")) && ( !inputEmail.getText().toString().equals("")) )
{
NetAsync(view);
}
else
{
Toast.makeText(getApplicationContext(),
"One or more fields are empty", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* Async Task check internet connection is working
**/
private class NetCheck extends AsyncTask<String,String,Boolean>
{
private ProgressDialog nDialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
nDialog = new ProgressDialog(Register.this);
nDialog.setMessage("Loading..");
nDialog.setTitle("Checking Network");
nDialog.setIndeterminate(false);
nDialog.setCancelable(true);
nDialog.show();
}
@Override
protected Boolean doInBackground(String... args){
/**
* Gets current device state and checks for working internet connection by trying Google.
**/
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL url = new URL("http://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setConnectTimeout(3000);
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
@Override
protected void onPostExecute(Boolean th){
if(th == true){
nDialog.dismiss();
new ProcessRegister().execute();
}
else{
nDialog.dismiss();
registerErrorMsg.setText("Error in Network Connection");
}
}
}
private class ProcessRegister extends AsyncTask<String, String, JSONObject> {
/**
* Defining Process dialog
**/
private ProgressDialog pDialog;
String email,password,fname,lname,dob;
@Override
protected void onPreExecute() {
super.onPreExecute();
inputPassword = (EditText) findViewById(R.id.passwordInput);
fname = inputFirstName.getText().toString();
lname = inputLastName.getText().toString();
email = inputEmail.getText().toString();
dobPicker = (DatePicker)findViewById(R.id.dobSelect);
dobResultView = (TextView)findViewById(R.id.dobResultView);
dob = Integer.toString(day)+"/"+Integer.toString(month+1)+"/"+Integer.toString(year);
password = inputPassword.getText().toString();
pDialog = new ProgressDialog(Register.this);
pDialog.setTitle("Contacting Servers");
pDialog.setMessage("Registering ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.registerUser(fname, lname, email, dob, password);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
/**
* Checks for success message.
**/
try {
if (json.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res = json.getString(KEY_SUCCESS);
String red = json.getString(KEY_ERROR);
if(Integer.parseInt(res) == 1){
pDialog.setTitle("Getting Data");
pDialog.setMessage("Loading Info");
registerErrorMsg.setText("Successfully Registered");
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
/**
* Removes all the previous data in the SQlite database
**/
UserFunctions logout = new UserFunctions();
logout.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_FIRSTNAME),json_user.getString(KEY_LASTNAME),json_user.getString(KEY_EMAIL),json_user.getString(KEY_DOB),json_user.getString(KEY_UID),json_user.getString(KEY_CREATED_AT));
/**
* Stores registered data in SQlite Database
* Launch Settings screen to complete setup
**/
Intent registered = new Intent(getApplicationContext(), AlarmActivity.class);
/**
* Close all views before launching Registered screen
**/
registered.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pDialog.dismiss();
startActivity(registered);
finish();
}
else if (Integer.parseInt(red) ==2){
pDialog.dismiss();
registerErrorMsg.setText("User already exists");
}
else if (Integer.parseInt(red) ==3){
pDialog.dismiss();
registerErrorMsg.setText("Invalid Email id");
}
}
else{
pDialog.dismiss();
registerErrorMsg.setText("Error occured in registration");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public void NetAsync(View view){
new NetCheck().execute();
}
// display current date
public void setCurrentDateOnView() {
dobPicker = (DatePicker) findViewById(R.id.dobSelect);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into datepicker
dobPicker.init(year, month, day, null);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener
= new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
dobPicker.init(year, month, day, null);
dob = " ("+Integer.toString(day)+"/"+Integer.toString(month+1)+"/"+Integer.toString(year)+")";
dobResultView.setText(dob);
}
};
}
视图代码:regiser.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6ec782"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/registerInstructView"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/registerBtn"
android:layout_alignParentTop="true"
android:layout_marginTop="33dp"
android:text="@string/registerInstructTxt"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/emailInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/passwordInput"
android:layout_below="@+id/registerInstructView"
android:layout_marginTop="23dp"
android:ems="10"
android:inputType="textEmailAddress"
android:textColor="#FFFFFF" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/firstNameInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/passwordInput"
android:layout_below="@+id/passwordInput"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/passwordInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/emailInput"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="textPassword"
android:textColor="#FFFFFF" />
<EditText
android:id="@+id/lastNameInput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/firstNameInput"
android:layout_below="@+id/firstNameInput"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/lastNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/lastNameInput"
android:layout_toLeftOf="@+id/lastNameInput"
android:text="@string/lastNameTxt"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/firstNameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/lastNameInput"
android:layout_toLeftOf="@+id/firstNameInput"
android:text="@string/firstNameTxt"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/register_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/registerBtn"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:textColor="#e30000"
android:padding="10dip"
android:textStyle="bold"/>
<TextView
android:id="@+id/passwordTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/firstNameInput"
android:layout_alignRight="@+id/firstNameView"
android:text="@string/passwordTxt"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/emailTxtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/emailInput"
android:layout_alignRight="@+id/passwordTxtView"
android:text="@string/emailTxt"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/dobResultView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dobSelect"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textColor="#FFFFFF" />
<DatePicker
android:id="@+id/dobSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dateBtn"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:calendarViewShown="false" />
<Button
android:id="@+id/registerBtn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/dateBtn"
android:layout_below="@+id/dobResultView"
android:layout_marginTop="63dp"
android:text="@string/registerBtnTxt"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/dateBtn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_below="@+id/lastNameInput"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="@string/dateBtnTxt"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/loginBtn"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/register_error"
android:layout_alignBottom="@+id/register_error"
android:layout_alignLeft="@+id/registerBtn"
android:text="@string/loginBtnTxt"
android:textColor="#FFFFFF" />
答案 0 :(得分:1)
更改此行
dobPicker.init(year, month, day, null);
dob = " ("+Integer.toString(month+1)+"/"+Integer.toString(day)+"/"+Integer.toString(year)+")";
dobResultView.setText(dob);
答案 1 :(得分:1)
dob = " ("+Integer.toString(day)+"/"+Integer.toString(month+1)+"/"+Integer.toString(year)+")";
而不是使用("+Integer.toString(month+1)+"/"+Integer.toString(day)+"/"+Integer.toString(year)+")";
答案 2 :(得分:1)
现在不推荐使用onCreateDialog。
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,day);
}
return null;
}
使用Fragment按如下方式显示对话框:
public class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
month = month + 1;
if (month < 10) {
txtBirthday.setText(day + "-" + "0" + month + "-" + year);
} else {
txtBirthday.setText(day + "-" + month + "-" + year);
}
}
}
它会按您的要求显示日期。
您可以像下面这样调用类(在任何onClickListener中)
txtBirthday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
});
txtBirthDay
是这里的文字视图。