2014-04-08 57 views
0

我有一個android活動,點擊EditText對話框將彈出一個日期選擇器。但是,當我設置日期時,它在編輯文本上顯示不正確。日曆EditText

這就是我講的一個例子:

這裏是我使用的代碼:

public class CreateTermWindow extends Activity { 

EditText tname, tstart, tend; 
Button tcreate; 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd", Locale.CANADA); 
Calendar calendar1 = Calendar.getInstance(); 
Calendar calendar2 = Calendar.getInstance(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_create_term_window); 

    tname = (EditText) findViewById(R.id.et_newTerm_name); 
    tstart = (EditText) findViewById(R.id.et_newTerm_start); 
    tend = (EditText) findViewById(R.id.et_newTerm_end); 
    tcreate = (Button) findViewById(R.id.btn_newTerm_create); 

    tstart.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      new DatePickerDialog(CreateTermWindow.this, c1, calendar1 
        .get(Calendar.YEAR), calendar1.get(Calendar.MONTH), 
        calendar1.get(Calendar.DATE)).show(); 
     } 
    }); 

    tend.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      new DatePickerDialog(CreateTermWindow.this, c2, calendar2 
        .get(Calendar.YEAR), calendar2.get(Calendar.MONTH), 
        calendar2.get(Calendar.DATE)).show(); 
     } 
    }); 

    tcreate.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      SQLiteDatabase db = new DatabaseHelper(getBaseContext()) 
        .getWritableDatabase(); 
      ContentValues values = new ContentValues(); 
      values.put("name", tname.getText().toString()); 
      values.put("start", tstart.getText().toString()); 
      values.put("end", tend.getText().toString()); 
      db.insert("termTable", null, values); 
      Toast.makeText(getApplicationContext(), "New Term Created!", 
        Toast.LENGTH_LONG).show(); 
     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.create_term_window, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

private void updateLabel(EditText e, Calendar c) { 
    e.setText(sdf.format(c.getTime())); 
} 

DatePickerDialog.OnDateSetListener c2 = new DatePickerDialog.OnDateSetListener() { 
    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     calendar2.set(Calendar.YEAR, year); 
     calendar2.set(Calendar.MONTH, monthOfYear); 
     calendar2.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
     updateLabel(tend, calendar2); 
    } 
}; 

DatePickerDialog.OnDateSetListener c1 = new DatePickerDialog.OnDateSetListener() { 
    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     calendar1.set(Calendar.YEAR, year); 
     calendar1.set(Calendar.MONTH, monthOfYear); 
     calendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
     updateLabel(tstart, calendar1); 
    } 
}; 
} 

回答

0

代替這段代碼,(你指updateLabel方法):

DatePickerDialog.OnDateSetListener c1 = new DatePickerDialog.OnDateSetListener() { 
@Override 
public void onDateSet(DatePicker view, int year, int monthOfYear, 
     int dayOfMonth) { 
    calendar1.set(Calendar.YEAR, year); 
    calendar1.set(Calendar.MONTH, monthOfYear); 
    calendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
    updateLabel(tstart, calendar1); 
} 

};

試試這個:

DatePickerDialog.OnDateSetListener c1 = new DatePickerDialog.OnDateSetListener() { 
@Override 
public void onDateSet(DatePicker view, int year, int monthOfYear, 
     int dayOfMonth) { 
    calendar1.set(Calendar.YEAR, year); 
    calendar1.set(Calendar.MONTH, monthOfYear); 
    calendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth); 
    tstart.setText(String.valueOf(year) + "/" 
+ String.valueOf(monthOfYear + 1) + "/" + String.valueOf(dayOfMonth)); 
} 

};