ページに 2 つのボタンがあります。ボタンを押すと、日付ピッカー ダイアログが開きます。日付ピッカーの [完了] をクリックすると、ボタンのテキストをユーザーが選択した日付に設定したいと考えています。
日付ピッカーの単一の関数を使用してこれを行いたいです。
私のコード:
final Button fromdate = (Button) dialog.findViewById(R.id.ButtonTripConfigureFrom);
fromdate.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
showDialog(0);
fromdate.setText(date);
}
});
final Button tilldate = (Button) dialog.findViewById(R.id.ButtonTripConfigureTo);
tilldate.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
showDialog(0);
tilldate.setText(date);
}
});
protected Dialog onCreateDialog(int id)
{
final Calendar c = Calendar.getInstance();
int myYear = c.get(Calendar.YEAR);
int myMonth = c.get(Calendar.MONTH);
int myDay = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(this,myDateSetListener,myYear, myMonth, myDay);
}
private DatePickerDialog.OnDateSetListener myDateSetListener = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int month, int day)
{
date = day + "/" + (month+1) +"/"+ year;
}
};
ただし、これを行うと、ユーザーが選択した値が表示されません。ボタンをもう一度押したときにのみ表示されます(ユーザーが日付ピッカーのDONEを押す前に、ボタンのsettextが実行されています)
私がチェックしたすべてのソリューションは、日付セット関数内の変更を示しています。しかし、2 つの異なるボタンがあるため、dateset 関数内でボタンの settext を使用することはできません。
日付セット関数によって値が返される方法、またはパラメーターを日付セットに渡してから日付セット内で if を使用する方法はありますか。