隐藏特定时间的布局

时间:2016-11-16 12:52:56

标签: java android time timepicker

我正在构建app.in,我希望将我的布局从上午8点隐藏到上午12点。我尝试使用线程和asynctask,但我不明白如何给出8到12之间的时间段。

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
     <de.hdodenhof.circleimageview.CircleImageView
            android:layout_width="40dp"
            android:layout_height="50dp"
            android:layout_marginLeft="3dp"
            android:scaleType="centerCrop"
            android:src="@drawable/vegitables"
            app:civ_border_color="#80000000"
            app:civ_border_width="1dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:text="Veg :"
            android:textColor="#F00"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="150dp"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

试试这个:

string([]byte[:n])

答案 1 :(得分:0)

首先,上午12点意味着午夜,而12点意味着中午。我假设你是中午,但这个解决方案很容易改变到任何时间边界。

您可以在活动中添加以下内容:

public class MyActivity extends AppCompatActivity {
    private Handler handler;
    private View layout;
    private Runnable updateVisibilityCallback;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.my_activity);

        handler = new Handler(Looper.getMainLooper());
        layout = findViewById(R.id.layout);
        updateVisibilityCallback = new Runnable() {
            @Override
            public void run() {
                updateTextVisibility();
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();

        updateTextVisibility();
    }

    @Override
    protected void onPause() {
        super.onPause();

        handler.removeCallbacks(updateVisibilityCallback);
    }

    private void updateTextVisibility() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        if (8 <= hour && hour <= 11) {
            layout.setVisibility(View.GONE);
        } else {
            layout.setVisibility(View.VISIBLE);
        }

        handler.postDelayed(updateVisibilityCallback, getVisibilityUpdateDelay());
    }

    private long getVisibilityUpdateDelay() {
        Calendar rightNow = Calendar.getInstance();
        int hour = rightNow.get(Calendar.HOUR_OF_DAY);

        Calendar nextUpdate = Calendar.getInstance();
        nextUpdate.set(Calendar.MINUTE, 0);
        nextUpdate.set(Calendar.SECOND, 0);
        nextUpdate.set(Calendar.MILLISECOND, 0);

        if (hour < 8) {
            nextUpdate.set(Calendar.HOUR_OF_DAY, 8);
        } else if (hour < 12) {
            nextUpdate.set(Calendar.HOUR_OF_DAY, 12);
        } else {
            nextUpdate.add(Calendar.DATE, 1);
            nextUpdate.set(Calendar.HOUR_OF_DAY, 8);
        }

        return nextUpdate.getTimeInMillis() - rightNow.getTimeInMillis();
    }
}

每次活动对用户可见时(在onResume中),您都会检查时间并更新布局的可见性。此外,您可以跟踪下次更新的时间 - 计算延迟时间为8:00或12:00,并使用Runnable发布Handler。只要您的用户停止关注活动,您就可以清除回调。