我有两个textview。当我点击它们时,它们会显示DateDialogBox ..这对我来说非常完美..我想将我的textview文本设置为所选日期..
public class ApplyLeaveFrag extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.apply_leave_fragment, container,
false);
TextView FROM = (TextView) view.findViewById(R.id.textView1);
TextView TO = (TextView) view.findViewById(R.id.textView2);
// I register setOnClickListener() on both of them:
FROM.setOnClickListener(this);
TO.setOnClickListener(this);
return view;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.textView1:
ShowCal();
break;
case R.id.textView2:
ShowCal();
break;
}
}
public void ShowCal() {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(this.getActivity(), this, year, month,
day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
//how can I set From or To's text From here..or any alternative
}
}
}
答案 0 :(得分:1)
以下将完成这项工作!
public class ApplyLeaveFrag extends Fragment implements OnClickListener {
static TextView FROM;
static TextView TO;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.apply_leave_fragment, container,
false);
FROM = (TextView) view.findViewById(R.id.textView1);
TO = (TextView) view.findViewById(R.id.textView2);
// I register setOnClickListener() on both of them:
FROM.setOnClickListener(this);
TO.setOnClickListener(this);
return view;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.textView1:
ShowCal();
break;
case R.id.textView2:
ShowCal();
break;
}
}
public void ShowCal() {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(this.getActivity(), this, year, month,
day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
//how can I set From or To's text From here..or any alternative
TO.setText("Date = " + year + "/" + "month"); // and so on
}
}
}
答案 1 :(得分:1)
public class ApplyLeaveFrag extends Fragment implements OnClickListener {
static TextView FROM,TO;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.apply_leave_fragment, container,
false);
FROM = (TextView) view.findViewById(R.id.textView1);
TO = (TextView) view.findViewById(R.id.textView2);
....
}
...
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
FROM.setText("from");
TO.setText("TO");
}
}
编辑:评论中的问题
声明一个静态布尔类变量
static boolean check=true;
点击
public void onClick(View v) {
switch (v.getId()) {
case R.id.textView1:
ShowCal();
check=true;
break;
case R.id.textView2:
ShowCal();
check=false;
break;
}
}
然后
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
if(check==true)
{
// set from text
}
else if(check ==false)
{
// set to text
}
}