版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yu75567218/article/details/52201224
当天如果未签到,则在签到的按钮右上角显示一个小红点,点击后红点消失。
在主页的fragment创建时判断今天有没有签到,没有签到则显示小红点:
private void initData() {
if (!SignUtil.isSignedToday()) {
// 今日没有签名则显示小圆点
mTitleBarView.showPoint();
}
}
签到的工具类:
public class SignUtil {
private static int ALREADY_CODE = 6307;
private static SignListener mSignListener;
/**
* 今天是否签到
* @return
*/
public static boolean isSignedToday() {
SharedPreferencesUtil sp = SharedPreferencesUtil.getUserSharedPreferences(KengApplication.getInstance().getApplicationContext(), UserUtil.getUserId());
long lastSign = sp.get(SharedPreferencesConstant.NAME_SIGN_DATE, 0L);
if (lastSign<=0 || !DateUtil.isToday(lastSign))
return false;
return true;
}
/**
* 签到
*/
public static void sign() {
if (isSignedToday()) {
KengToast.show(R.string.already_sign);
return;
}
KengHttpBuilder builder = new KengHttpBuilder();
builder.url(HttpUtil.BASE_URL + "client/user_sign_j.ss");
builder.put(UserState.ID, UserUtil.getUserId());
HttpUtil.sendRequest(builder, new KengHttpCallback() {
@Override
public void onSuccess(JSONObject data) {
KengToast.show(KengApplication.getInstance().getApplicationContext().getString(R.string.sign_success, data.optInt("continuityTimes")));
// 保存签到时间到本地
SharedPreferencesUtil sp = SharedPreferencesUtil.getUserSharedPreferences(KengApplication.getInstance().getApplicationContext(), UserUtil.getUserId());
sp.put(SharedPreferencesConstant.NAME_SIGN_DATE, System.currentTimeMillis());
if (mSignListener != null) {
// 签到成功回调
mSignListener.onSuccess();
}
}
@Override
public void onFail(int code, String msg) {
KengToast.show(msg);
if (code == ALREADY_CODE) {
SharedPreferencesUtil sp = SharedPreferencesUtil.getUserSharedPreferences(KengApplication.getInstance().getApplicationContext(), UserUtil.getUserId());
sp.put(SharedPreferencesConstant.NAME_SIGN_DATE, System.currentTimeMillis());
}
}
});
}
public static void setSignListener(SignListener mSignListener) {
SignUtil.mSignListener = mSignListener;
}
/**
* 签到回调
*/
public interface SignListener {
void onSuccess();
}
}
在主页的fragment中的签到点击事件:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_right:
mTitleBarView.hidePoint();
SignUtil.sign();
break;
}
}
小红点在标题栏中的布局:
<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="@dimen/titlebar_height"
android:background="@color/titlebar_color">
<TextView
android:id="@+id/tv_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/margin_default"
android:textColor="@color/font_white"
android:textSize="@dimen/font_small"
/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="@dimen/margin_default"
android:textColor="@color/font_white"
android:textSize="@dimen/font_middle"
/>
<TextView
android:id="@+id/tv_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="@dimen/margin_default"
android:textColor="@color/font_white"
android:textSize="@dimen/font_small"
/>
<!-- 小红点-->
<ImageView
android:id="@+id/iv_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle_point"
android:layout_alignRight="@id/tv_right"
android:layout_alignTop="@id/tv_right"
android:visibility="gone"
/>
</RelativeLayout>
小红点是用shape绘制的circle_point.xml:
<xml version="1.0" encoding="utf-8">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/holo_red_light" />
<size android:width="@dimen/circle_size" android:height="@dimen/circle_size" />
</shape>
未签到界面:

已签到界面:
