更改日期的Android DatePickerDialog

时间:2015-05-30 08:26:07

标签: android

我想在datePickerDialog中修复月份和年份,但希望用户自己选择日期。可能吗? 例如:DatePickerDialog将在5月和2015年修复,用户现在只能更改1到31之间的日期

3 个答案:

答案 0 :(得分:1)

我通过以下方式解决了我的问题:

<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
.green {
    background: #4DDB4D;
    color: black;
}
.red {
    background: red;
}
</style>
</head>
</html>
<?php 
    $conn = new mysqli("localhost","root","","db_dat");
    $havepassport='';
$sql = "SELECT * from upload WHERE having_passport='yes' OR having_passport='no'";
$result = $conn -> query($sql);
$havepassport='';
 $havepassport .='<table>';
 $havepassport .='<tr>'.'<th>'."Name".'</th>'.'<th>'."Having Passport".'</th>'.'</tr>';


while ($row = $result -> fetch_assoc()) {
    $passportClass = $row['having_passport'] == 'yes' ? 'green' : 'red';

    $havepassport .='<tr class='.$passportClass.'>'.'<td>';
    $havepassport .= '<a href=>'. $row['emp_name'].'</a>';
    $havepassport .= '</td>'.'<td>'. $row['having_passport'].'</td>'.'</tr>';
}
    $havepassport.='</table>';
    echo $havepassport;
?>

现在我的日期选择器对话框已修复为月份和年份,,,用户只能更改1到30或31之间的日期

答案 1 :(得分:0)

您可以使用以下代码行

 public class First extends Activity {
DatePicker _date;
Date date_current;
Date date_future;
int days_count=0;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first);
    Calendar ci = Calendar.getInstance();
    String CiDateTime = ci.get(Calendar.YEAR) + "-" + 
            (ci.get(Calendar.MONTH) + 1) + "-" + 
            ci.get(Calendar.DAY_OF_MONTH);

    SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");


    try {
        date_current =  f.parse(CiDateTime);
        long milliseconds = date_current.getTime();
        _date.setMinDate(milliseconds);//here is _date is DatePicker Object

        int monthMaxDays = ci.getActualMaximum(Calendar.DAY_OF_MONTH); 
        if(monthMaxDays>ci.get(Calendar.DAY_OF_MONTH))
        {
            days_count = monthMaxDays-ci.get(Calendar.DAY_OF_MONTH);
            String CiDateMax = ci.get(Calendar.YEAR) + "-" + 
                    (ci.get(Calendar.MONTH) + 1) + "-" + 
                    ci.get(Calendar.DAY_OF_MONTH)+days_count;
            date_future =  f.parse(CiDateMax);
            long milliseconds_MAx = date_future.getTime();
            _date.setMaxDate(milliseconds_MAx);
        }
        else
        {
            _date.setMaxDate(milliseconds);
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }
}

答案 2 :(得分:0)

您可以从DatePickerDialog获取基础DatePicker(通过简单地调用getDatePicker())并使用以下命令设置其边界: