开发者

Runtime Textview Problem in Datepicker dialog

Is it possible to create a single run time Textview and upon OnClick show a DatePicker Dialog and when date is set show the result in the same Te开发者_如何学JAVAxtView?


 private int mYear;
 private int mMonth;
 private int mDay;
 private TextView mDateDisplay;
 static final int DATE_DIALOG_ID = 0;

 public void onCreate(Bundle savedInstaneState){
        super.onCreate(savedInstaneState);

 setContentView(R.layout.main);  

 mDateDisplay = (TextView) findViewById(R.id.datepicker); 

 mDateDisplay.setOnClickListener(this);


    // 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);


    }

public void onClick(View v) {

     showDialog(DATE_DIALOG_ID);         
}   
private void updateDisplay() {
    this.mDateDisplay.setText(
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" "));
}
 private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };
        @Override
        protected Dialog onCreateDialog(int id) {
           switch (id) {
           case DATE_DIALOG_ID:
              return new DatePickerDialog(this,
                        mDateSetListener,
                        mYear, mMonth, mDay);
           }
           return null;
        }

xml is

<TextView android:text="Select date"
  android:id="@+id/datepicker" 
  android:layout_width="wrap_content" 
  android:textStyle="bold"
  android:textSize="28dip"
  android:editable = "true"
  android:clickable="true"
  android:layout_height="wrap_content"/>


I got answer of this... we have to pass the Text View where we want to display like if we have set "Pick date" before clicking so after showing dialog box the date that we want to set in the text View should be passed in the same Text View by referencing same Text View object....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜