DatePicker自定义标题和多个时间选择框的处理

这篇博客展示了如何在Android应用中实现自定义DatePicker标题并处理多个时间选择框。通过创建自定义DatePickerDialog,可以设置不同的提示标题,并在按钮点击事件中展示所选日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

直接上代码了:

package dateandtimer;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import com.example.ctroltest.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class DatePickerTest extends Activity {
 private TextView datedisp,cus_date_disp1,cus_date_disp2;
 private Button dateselectbtn,cus_date_select1btn,cus_date_select2btn;
 private int myear,mmonth,mday;
    private DatePickerDialog.OnDateSetListener dateListener,cusSetListener01,cusSetListener02;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.date_picker_test);
  datedisp=(TextView)findViewById(R.id.datedisp);
  dateselectbtn=(Button)findViewById(R.id.dateselectbtn);
  Calendar myCalendar=Calendar.getInstance(Locale.CHINA);
  Date mydate=new Date();
  myCalendar.setTime(mydate);
  myear=myCalendar.get(Calendar.YEAR);
  mmonth=myCalendar.get(Calendar.MONTH);
  mday=myCalendar.get(Calendar.DAY_OF_MONTH);  
  dateselectbtn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    settime(dateListener, datedisp, "时间选择........");
    
   }
  });
  
  cus_date_disp1=(TextView)findViewById(R.id.cus_date_disp1);
  cus_date_select1btn=(Button)findViewById(R.id.cus_date_select1btn);
  cus_date_select1btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    settime(cusSetListener01, cus_date_disp1, "自定义时间选择1....");
   }
  });
  
  
  
  
  cus_date_disp2=(TextView)findViewById(R.id.cus_date_disp2);
  cus_date_select2btn=(Button)findViewById(R.id.cus_date_select2btn);
  cus_date_select2btn.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
    
    // TODO Auto-generated method stub
    settime(cusSetListener02, cus_date_disp2, "自定义时间选择2....");
    
   }
  });
  
  
 }
 

 
 

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.date_picker_test, menu);
  return true;
 }
 
//复写一个类继承DatePickerDialog,自定义时间对话框的标题

 public class CustomDatePickerDiaglog extends DatePickerDialog{

  private String titlecontent;

  public CustomDatePickerDiaglog(Context context,
    OnDateSetListener callBack, int year, int monthOfYear,
    int dayOfMonth,String title) {
   super(context, callBack, year, monthOfYear, dayOfMonth);
   // TODO Auto-generated constructor stub
   titlecontent=title;
   setTitle(title);
   
  }

  /* (non-Javadoc)
   * @see android.app.DatePickerDialog#onDateChanged(android.widget.DatePicker, int, int, int)
   */
  @Override
  public void onDateChanged(DatePicker view, int year, int month, int day) {
   // TODO Auto-generated method stub
   super.onDateChanged(view, year, month, day);
   setTitle(titlecontent);
  }
  
  
 }
 
 //定义按钮选择事件调用的方法。


 private void settime(DatePickerDialog.OnDateSetListener dListener,
   final TextView tv,String title){
  
  dListener
  =new DatePickerDialog.OnDateSetListener(){

   @Override
   public void onDateSet(DatePicker view, int year, int monthOfYear,
     int dayOfMonth) {
    // TODO Auto-generated method stub
    tv.setText("你选择了"+year+"年"+(monthOfYear+1) +"月"+dayOfMonth+"日");
   }
      
   
  };
  
  CustomDatePickerDiaglog customDatePickerDiaglog=
    new CustomDatePickerDiaglog(
      DatePickerTest.this, dListener, myear, mmonth, mday, title);
  customDatePickerDiaglog.show();
  
  
 }

}

 

 

布局文件:

 

<LinearLayout 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:orientation="vertical" >
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="日期为:" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/datedisp"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择日期"
        android:id="@+id/dateselectbtn"
 
        />
    </LinearLayout>
   
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义日期1为:" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cus_date_disp1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义选择日期1"
        android:id="@+id/cus_date_select1btn"
 
        />
    </LinearLayout>
   

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义日期2为:" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cus_date_disp2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义选择日期2"
        android:id="@+id/cus_date_select2btn"
 
        />
    </LinearLayout>


</LinearLayout>

 

效果图:

 


 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值