我正在编写带有片段的标签栏程序。我有4个片段,一个标签栏正在处理它们。我的问题是我无法使用片段生命周期方法(或者我无法学习)。我有一个运行一些代码的按钮,我想计算从输入时间到现在已经过去了多少时间。该程序会计算时间,但不会覆盖textview,我更改了片段并返回了相关片段,它已经起作用了,但我想像您所知道的那样突然起作用:)。我希望我能向您解释,因为我不会说自己具备良好的英语水平。谢谢!
public class fragmentTimer extends Fragment {
@Nullable
TextView textView;
EditText dateText;
Button buttonTime;
long b;
SharedPreferences preferences;
int beforeAfter;
boolean isPast;
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//buttonTime = (Button) view.findViewById(R.id.button);
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmenttimer, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final TextView textView = (TextView) getView().findViewById(R.id.textView4);
dateText = getView().findViewById(R.id.dateText);
buttonTime = (Button) getView().findViewById(R.id.button);
buttonTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Date userDate = new Date();
Date currDate = new Date();
System.out.printf("%1$s %2$tB %2$td, %2$tY", "Due date:", currDate);
String str = String.format("Current Date/Time : %tc", currDate);
//textView.setText(str);
String dateString;
String fromDate = "01/07/2019";
dateString = dateText.getText().toString();
String fromDate2 = dateString;
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dtt = null;
try {
dtt = df.parse(fromDate2);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date ds = new java.sql.Date(dtt.getTime());
beforeAfter = currDate.compareTo(dtt);
if(beforeAfter > 0){
b = currDate.getTime() - dtt.getTime();
isPast = true;
}else if(beforeAfter < 0){
b = dtt.getTime() - currDate.getTime();
isPast = false;
}
}
});
int saniye,dakika,saat,gun;
b= b/1000;
saniye = (int) (b % 60);
b = b / 60;
dakika = (int) (b % 60);
b = b / 60;
saat = (int) (b % 24);
b= b / 24;
gun = (int) b;
String durum = "";
if(isPast == true){
durum = "Geçti";
}
else if(isPast == false){
durum = "Kaldı";
}
//textView.setText("saniye :" + saniye + " dakika :" + dakika + "\n saat :" + saat + "gün :" + gun);
textView.setText(+gun + " Gün " + saat + " Saat " + dakika + " Dakika " + saniye + " Saniye " + durum + " ");
}
public void buttonTime9(View view) {
Date userDate = new Date();
Date currDate = new Date();
System.out.printf("%1$s %2$tB %2$td, %2$tY", "Due date:", currDate);
String str = String.format("Current Date/Time : %tc", currDate);
textView.setText(str);
String dateString;
String fromDate = "19/05/2009";
dateString = dateText.getText().toString();
String fromDate2 = dateString;
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date dtt = null;
try {
dtt = df.parse(fromDate);
} catch (ParseException e) {
e.printStackTrace();
}
java.sql.Date ds = new java.sql.Date(dtt.getTime());
long b = currDate.getTime() - dtt.getTime();
textView.setText("Fark" + b);
System.out.println("fark " + b);
}
}