天天看點

日期選擇對話框

日期選擇對話框
日期選擇對話框

日期選擇對話框

.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"     tools:context=".MainActivity" >

    <Button          android:id="@+id/mybut"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="設定日期"/>          <TextView          android:id="@+id/mytxt"         android:layout_width="wrap_content"         android:layout_height="wrap_content"/>

</RelativeLayout>

.java public class MainActivity extends Activity { private Button mybut=null; private TextView txt=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.mybut=(Button) super.findViewById(R.id.mybut); this.txt=(TextView) super.findViewById(R.id.mytxt); this.mybut.setOnClickListener(new OnClickListenerImp()); } public class OnClickListenerImp implements OnClickListener{

public void onClick(View arg0) { Dialog dialog=new DatePickerDialog(MainActivity.this,new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { MainActivity.this.txt.setText("修改的日期為:"+year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日"); } },1990,8,16);//表示預設的年月日 dialog.show(); } }

}