• 第二阶段冲刺第三天


    摘要:今天继续写了一些后台,功能实现差不多了,点击签到可以签到

    代码:

    package com.lh.std_everything.ui.sign;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.FragmentManager;
    
    import android.os.Bundle;
    import android.view.View;
    import android.view.Window;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import com.lh.std_everything.R;
    import com.lh.std_everything.ui.sign.SignViewX.ResolutionUtil;
    import com.lh.std_everything.ui.sign.SignViewX.SignAdapter;
    import com.lh.std_everything.ui.sign.SignViewX.SignEntity;
    import com.lh.std_everything.ui.sign.SignViewX.SignView;
    import com.zhy.autolayout.AutoLinearLayout;
    
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.List;
    
    public class SignActivity extends AppCompatActivity {
    
    
        private List<SignEntity> data;
        private TextView headTitle;
        private TextView tvSignState;
        private LinearLayout llSign;
        private TextView tvSignData;
        private TextView tvDate;
        private SignView signView;
        private TextView tvScore;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            ResolutionUtil.getInstance().init(this);
            setContentView(R.layout.activity_sign);
            initView();
            initData();
        }
    
    
        private void initView() {
            headTitle = findViewById(R.id.head_title);
            tvSignState = findViewById(R.id.tv_sign_state);
            llSign = findViewById(R.id.ll_sign);
            tvSignData = findViewById(R.id.tv_sign_data);
            tvDate = findViewById(R.id.tv_date);
            signView = findViewById(R.id.activity_main_cv);
            tvScore = findViewById(R.id.tv_score);
    
    
        }
    
    
        private void initData() {
            headTitle.setText("每日签到");
            if (signView != null) {
                signView.setOnTodayClickListener(onTodayClickListener);
            }
            onReady();
    
            llSign.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSign();
                }
            });
    
    
        }
    
        private void onReady() {
            Calendar calendar = Calendar.getInstance();
            int month = calendar.get(Calendar.MONTH);
            tvSignData.setText("您已经连续签到2天");
            tvScore.setText(String.valueOf(305));
            tvDate.setText(String.valueOf(calendar.get(Calendar.YEAR)) + "-" + String.valueOf(calendar.get(Calendar.MONTH) + 1));
            Calendar calendarToday = Calendar.getInstance();
            int dayOfMonthToday = calendarToday.get(Calendar.DAY_OF_MONTH);
            data = new ArrayList<>();
            //已经签到
            List<Integer> integers = new ArrayList<>();
            integers.add(1);
            integers.add(2);
            //奖品设置
            List<Integer> integersX = new ArrayList<>();
            integersX.add(5);
            integersX.add(10);
            for (int i = 0; i < 30; i++) {
                SignEntity signEntity = new SignEntity();
                if (dayOfMonthToday == i + 1) {
                    signEntity.setDayType(2);
                } else {
    
                    if (integersX.contains(i)) {
                        signEntity.setDayType(4);
                    } else {
                        signEntity.setDayType((integers.contains(i + 1)) ? 0 : 1);
                    }
    
                    data.add(signEntity);
                }
            }
            SignAdapter signAdapter = new SignAdapter(data);
            signView.setAdapter(signAdapter);
        }
    
    
        private void onSign() {
            FragmentManager fragmentManager = getSupportFragmentManager();
            SignDialogFragment signDialogFragment = SignDialogFragment.newInstance(15);
            signDialogFragment.setOnConfirmListener(onConfirmListener);
            signDialogFragment.show(fragmentManager, SignDialogFragment.TAG);
        }
    
        private void signToday() {
            data.get(signView.getDayOfMonthToday() - 1).setDayType(SignView.DayType.SIGNED.getValue());
            signView.notifyDataSetChanged();
            llSign.setEnabled(false);
            tvSignState.setText(R.string.have_signed);
            int score = Integer.valueOf((String) tvScore.getText());
            tvScore.setText(String.valueOf(score + 15));
        }
    
        private SignView.OnTodayClickListener onTodayClickListener = new SignView.OnTodayClickListener() {
            @Override
            public void onTodayClick() {
                onSign();
            }
        };
    
    
        private SignDialogFragment.OnConfirmListener onConfirmListener = new SignDialogFragment.OnConfirmListener() {
            @Override
            public void onConfirm() {
                signToday();
            }
        };
    
    }
    View Code
    package com.lh.std_everything.ui.sign;
    
    import android.annotation.SuppressLint;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.text.Spannable;
    import android.text.SpannableString;
    import android.text.style.AbsoluteSizeSpan;
    import android.util.TypedValue;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatDialog;
    import androidx.appcompat.app.AppCompatDialogFragment;
    import androidx.appcompat.widget.AppCompatButton;
    
    import com.lh.std_everything.R;
    import com.lh.std_everything.ui.sign.SignViewX.ResolutionUtil;
    import com.lh.std_everything.ui.sign.SignViewX.StringUtil;
    
    
    /**
     * SignDialogFragment
     */
    public class SignDialogFragment extends AppCompatDialogFragment {
        public static final String TAG = "SIGN";
        public static final String ARG_SCORE_NUMBER = "ARG_SCORE_NUMBER";
        private int score;
        private ResolutionUtil resolutionUtil;
    
        public SignDialogFragment() {
            resolutionUtil = ResolutionUtil.getInstance();
        }
    
        public static SignDialogFragment newInstance(int score) {
            SignDialogFragment signDialogFragment = new SignDialogFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(ARG_SCORE_NUMBER, score);
            signDialogFragment.setArguments(bundle);
            return signDialogFragment;
        }
    
    
        @SuppressLint("InflateParams")
        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            View contentView = getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_sign, null);
            getParam();
            initView(contentView);
    
            AppCompatDialog appCompatDialog = new AppCompatDialog(getContext(), R.style.DialogNoTitle);
            appCompatDialog.setContentView(contentView, new ViewGroup.LayoutParams(resolutionUtil.formatHorizontal(961),
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            appCompatDialog.setCanceledOnTouchOutside(false);
            return appCompatDialog;
        }
    
        private void getParam() {
            if (getArguments() != null) {
                score = getArguments().getInt(ARG_SCORE_NUMBER, 0);
            }
        }
    
        @SuppressLint("RestrictedApi")
        private void initView(View root) {
            TextView tvMessage = (TextView) root.findViewById(R.id.dialog_sign_tv_message);
            TextView tvScoreNumber = (TextView) root.findViewById(R.id.dialog_sign_tv_score_number);
            AppCompatButton btnSure = root.findViewById(R.id.dialog_sign_btn_sure);
    
            int scoreLength = StringUtil.getStringLength(score);
            String numberStr = String.format(getString(R.string.much_score_int), score);
    
            SpannableString spannableString = new SpannableString(numberStr);
            spannableString.setSpan(new AbsoluteSizeSpan(resolutionUtil.formatVertical(120)), 0, scoreLength, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            spannableString.setSpan(new AbsoluteSizeSpan(resolutionUtil.formatVertical(39)), scoreLength, numberStr.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            tvScoreNumber.setText(spannableString);
    
            btnSure.setSupportBackgroundTintList(getResources().getColorStateList(R.color.color_user_button_submit));
            btnSure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDialog().dismiss();
                    if (onConfirmListener != null) {
                        onConfirmListener.onConfirm();
                    }
                }
            });
    
            //------------------------------分辨率适配------------------------------------
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = resolutionUtil.formatVertical(67);
            layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
            tvMessage.setLayoutParams(layoutParams);
            tvMessage.setTextSize(TypedValue.COMPLEX_UNIT_PX, resolutionUtil.formatVertical(44));
    
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = resolutionUtil.formatVertical(39);
            layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
            tvScoreNumber.setLayoutParams(layoutParams);
    
            layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.topMargin = resolutionUtil.formatVertical(61);
            layoutParams.bottomMargin = resolutionUtil.formatVertical(67);
            layoutParams.leftMargin = layoutParams.rightMargin = resolutionUtil.formatHorizontal(73);
            btnSure.setLayoutParams(layoutParams);
            btnSure.setTextSize(TypedValue.COMPLEX_UNIT_PX, resolutionUtil.formatVertical(48));
        }
    
        private OnConfirmListener onConfirmListener;
    
        public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
            this.onConfirmListener = onConfirmListener;
        }
    
        public interface OnConfirmListener {
            void onConfirm();
        }
    }
    View Code

  • 相关阅读:
    array与xml转换实现(转)
    设计模式之: 策略模式
    设计模式之: 代理模式
    设计模式之: 状态模式
    dedecms分页
    dedecms导出csv文件
    假如项目中使用到了多 表查询,怎么办?
    git忽略某个文件夹
    git忽略某个文件
    无极限分类
  • 原文地址:https://www.cnblogs.com/dd110343/p/13059776.html
Copyright © 2020-2023  润新知