Android (3) DatePickerDialog 選擇日期
1. 添加一個 Button 和 TextView:
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/dateText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Date" android:textSize="30sp"/> <Button android:id="@+id/dateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Date" /> </LinearLayout>
Button 用來選擇日期,TextView 用來顯示資料。
2. Source code:
先在原本的程式中找到我們的物件,並幫 Button 註冊一個 Listener。
public class MainActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView dateText = (TextView)findViewById(R.id.dateText); final Button dateButton = (Button)findViewById(R.id.dateButton); dateButton.setOnClickListener(new MyDatePicker(dateText)); } }
Listener 實做 OnClickListener 和 OnDateSetListener 的方法。 按下按鍵時會跳出可以選擇的日曆,選擇完日期後會觸發更新。
private class MyDatePicker implements View.OnClickListener, DatePickerDialog.OnDateSetListener { private TextView mDateText; public MyDatePicker(TextView dateText) { mDateText = dateText; } @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog( MainActivity.this, // context this, // listener calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); dialog.show(); } @Override public void onDateSet(android.widget.DatePicker view, int year, int month, int day) { String des = String.format("%s-%s-%s", year, month, day); mDateText.setText(des); } }
3. 完整程式碼:
package com.example.ben.androidtest; import android.app.DatePickerDialog; import android.icu.util.Calendar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView dateText = (TextView)findViewById(R.id.dateText); final Button dateButton = (Button)findViewById(R.id.dateButton); dateButton.setOnClickListener(new MyDatePicker(dateText)); } private class MyDatePicker implements View.OnClickListener, DatePickerDialog.OnDateSetListener { private TextView mDateText; public MyDatePicker(TextView dateText) { mDateText = dateText; } @Override public void onClick(View v) { Calendar calendar = Calendar.getInstance(); DatePickerDialog dialog = new DatePickerDialog( MainActivity.this, this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); dialog.show(); } @Override public void onDateSet(android.widget.DatePicker view, int year, int month, int day) { String des = String.format("%s-%s-%s", year, month, day); mDateText.setText(des); } } }
留言
張貼留言