日、時間、分、秒でカウントダウンタイマーを作成しました。アクティビティとして使用すると正しく動作しますが、これをフラグメントに入れる必要があり、SetContentView を使用できません。どうすればそれを機能させることができますか?どんな助けでも大歓迎です。ありがとう!
これが私のコードです:
public class FragmentOne extends Fragment {
private TextView tvDay, tvHour, tvMinute, tvSecond, tvEvent;
private LinearLayout linearLayout1, linearLayout2;
private Handler handler;
private Runnable runnable;
public static Fragment newInstance(Context context) {
FragmentOne f = new FragmentOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, container,false);
linearLayout1 = (LinearLayout) getActivity().findViewById(R.id.ll1);
linearLayout2 = (LinearLayout) getActivity().findViewById(R.id.ll2);
tvDay = (TextView) getActivity().findViewById(R.id.txtTimerDay);
tvHour = (TextView) getActivity().findViewById(R.id.txtTimerHour);
tvMinute = (TextView) getActivity().findViewById(R.id.txtTimerMinute);
tvSecond = (TextView) getActivity().findViewById(R.id.txtTimerSecond);
tvEvent = (TextView) getActivity().findViewById(R.id.tvevent);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
Date futureDate = dateFormat.parse("2016-2-19");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
tvDay.setText("" + String.format("%02d:", days));
tvHour.setText("" + String.format("%02d:", hours));
tvMinute.setText("" + String.format("%02d:", minutes));
tvSecond.setText("" + String.format("%02d", seconds));
} else {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
tvEvent.setText("Event Start");
handler.removeCallbacks(runnable);
handler.removeMessages(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 0);
return root;
}
}
そして、同じための私のXML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background" >
<LinearLayout
android:id="@+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="@+id/tvevent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:singleLine="true"
android:text="Android Event Start"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="#00000000"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="horizontal"
android:visibility="visible" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="50sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="50sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#00FFFFFF"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textSize="50sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>