大家好我收到一個錯誤,onClick
在點擊兩次後觸發,我希望它在第一次點擊時執行。在這段代碼中,我用一個日期選擇器來顯示日期,在用戶選擇一個日期並按下確定按鈕時,該日期必須在TextView中顯示。onClick在onclick觸發兩次點擊後,即當我點擊第三次按OK按鈕時觸發
這是我的代碼
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);
}
});
}}
的[DatePicker.OnDateChangedListener叫了兩聲]可能的複製(http://stackoverflow.com/questions/:
即
可以用於填充
txt
場創建方法12436073/datepicker-ondatechangedlistener-調用兩次) –在這裏我的問題是沒有發射達到兩次我點擊確定按鈕後,第三次我點擊確定按鈕它顯示...有巨大的差異 – naveen