0

我用天、小时、分钟和秒做了一个倒计时。用作 Activity 时它可以正常工作,但现在我必须将它放在一个片段中,我不能使用 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>
4

2 回答 2

1

但现在我必须把它放在一个片段中,我不能使用 SetContentView

getActivity()return ActivityContext 并调用findViewByIdusinggetActivity意味着从 Activity 布局访问 View 的,而不是从onCreateView方法返回的 Fragment。

因此,要使其正常工作,请使用root而不是getActivity()FragmentLayout 访问 View :

linearLayout1 = (LinearLayout) root.findViewById(R.id.ll1);

对访问其他视图执行相同操作。

于 2016-02-11T05:30:02.750 回答
0

用这个替换 getActivity() -

     ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, container,false);
     linearLayout1 = (LinearLayout) root.findViewById(R.id.ll1);
......................
于 2016-02-11T05:30:18.503 回答