两次点击后点击onDateChanged on,当我第三次点击确定按钮时

时间:2015-12-23 12:26:54

标签: android datepicker

大家好我收到onClick点击两次后发出的错误,我想让它在第一次点击时执行。在这段代码中,我选择了一个日期选择器来显示日期,用户选择一个日期并按下确定按钮,日期必须显示在TextView中。

这是我的代码

public class TimePicker extends Activity{
protected static OnDateChangedListener OnDateChangedListener;
private int year;
private int month;
private int day;
private String Date;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.time_picker);   
    Calendar cal = Calendar.getInstance();
    year = cal.get(Calendar.YEAR);
    month = cal.get(Calendar.MONTH);
    day = cal.get(Calendar.DAY_OF_MONTH);
    final DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);      


    Button b = (Button) findViewById(R.id.btnOk);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub  
            OnDateChangedListener = new OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker v, int selectedYear, int selectedMonth, int selectedDay) {
                    // TODO Auto-generated method stub
                    year = selectedYear;
                    month = selectedMonth;
                    day = selectedDay;
                }
            };  
            dp.init(year, month, day, OnDateChangedListener);
            StringBuilder sb = new StringBuilder();
            sb.append(day).append("-").append(month+1).append("-").append(year);
            Date = sb.toString();
            TextView txt = (TextView) findViewById(R.id.textViewTime);
            txt.setText(Date);  
        }
    }); 

} }

1 个答案:

答案 0 :(得分:1)

将此行dp.init(year, month, day, OnDateChangedListener);移到OnDateChangedListener实例化下方。它第二次被调用,因为它刚刚第一次被启动。

您还应该在txt字段中设置setText,并在为其分配OnDateChanged新值后在Date内致电setText。移除onClickOnDateChangedListener = new OnDateChangedListener() { @Override public void onDateChanged(DatePicker v, int selectedYear, int selectedMonth, int selectedDay) { // TODO Auto-generated method stub year = selectedYear; month = selectedMonth; day = selectedDay; displayDate(); } }; dp.init(year, month, day, OnDateChangedListener); displayDate(); 的来电。

txt

您可以创建一个填充private void displayDate() { StringBuilder sb = new StringBuilder(); sb.append(day).append("-").append(month+1).append("-").append(year); Date = sb.toString(); TextView txt = (TextView) findViewById(R.id.textViewTime); txt.setText(Date); } 字段的方法:

        $users = User::with([
            'roles' => function ($query) use ($request) {
                if (is_null($request['role'])) {
                    $query;
                } else {
                    $query->where('id', $request['role']);
                }
            },

            'group' => function ($query) use ($request) {
                if (is_null($request['group'])) {
                    $query;
                } else {
                    $query->where('id', $request['group']);
                }
            }
        ])->status($request['status'])
          ->paginate(20);