我试图在片段中的textview中设置日期。问题是,在选择日期并单击对话框的确定按钮后,它不会立即将日期设置为textview。再次单击设置日期按钮时,在文本视图中设置日期时即可。
ButtonFragment
public class ButtonFragment extends Fragment {
Button date;
TextView datetext;
Date dateInterface;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.button_fragment, container, false);
date = (Button) view.findViewById(R.id.date);
datetext = (TextView) view.findViewById(R.id.datetext);
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dateInterface.setDate(datetext);
}
});
datetext = (TextView) view.findViewById(R.id.datetext);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
dateInterface = (Date) activity;
}catch(Exception e) {
}
}
public interface Date{
public void setDate(TextView textView);
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements ButtonFragment.Date {
Calendar c = Calendar.getInstance();
TextView display;
int cday, cmonth, cyear;
String date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ButtonFragment buttonFragment = new ButtonFragment();
fragmentTransaction.add(R.id.fragment_container, buttonFragment);
fragmentTransaction.commit();
}
public void setDate(String date) {
this.date = date;
}
public String getDate() {
return this.date;
}
DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
cday = dayOfMonth;
cmonth = monthOfYear + 1;
cyear = year;
}
};
@Override
public void setDate(TextView textView) {
new DatePickerDialog(MainActivity.this, d,
c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
.get(Calendar.DAY_OF_MONTH)).show();
date = "Choosen date is :" + cday + "/" + cmonth + "/"
+ cyear;
textView.setText(date);
}
}
答案 0 :(得分:1)
更改代码如下
DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
cday = dayOfMonth;
cmonth = monthOfYear + 1;
cyear = year;
date = "Choosen date is :" + cday + "/" + cmonth + "/"
+ cyear;
txtNow.setText(date);// set the text using the global pointer
}
};
TextView txtNow;// this will be global pointer to the textView passed
@Override
public void setDate(TextView textView) {
txtNow = textView;// initilize the Global pointer
new DatePickerDialog(MainActivity.this, d,
c.get(Calendar.YEAR), c.get(Calendar.MONTH), c
.get(Calendar.DAY_OF_MONTH)).show();
}
现在应该可以使用
答案 1 :(得分:0)
我将简单回答一下,只需在onViewCreated / onCreateView等任何方法之外使用它们。例如:将这些方法添加为类/片段的公共/私有/受保护方法之一:
DatePickerDialog.OnDateSetListener d = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
txtNow.setText(year + "/" + monthOfYear+1 "/" + dayOfMonth);// set the text using the global pointer
}
};