[Android Studio] 个人主页界面的实现

目录

接上篇:底部导航栏的实现

1.个人主页界面的设计:fragment_blank.xml

2.个人主页功能的实现类:BlankFragment.java

3.每日签到功能的实现

4.实现效果

5.图片素材


接上篇:[Android Studio] 底部导航栏的实现

1.个人主页界面的设计:fragment_blank.xml




    
    

    
    

    

    

    

        

        
            
            
            
        
    

    


        

        

            

            

            

        

        

        

            

            

            

        

        

        

            

            

            

        

        

        

            

            

            

        

        


    

2.个人主页功能的实现类:BlankFragment.java

package com.example.ji_zhang_ben.ui.blank;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.ji_zhang_ben.R;


public class BlankFragment extends Fragment implements View.OnClickListener{

    TextView signup,clock_day;//每日签到,显示签到的天数
    RelativeLayout budget_btn,usehelp_btn,about_btn,setting_btn;  //预算中心,使用帮助,关于我们,设置
    View sigup_view;  //签到弹窗视图
    AlertDialog write;
    SharedPreferences sp;
    SharedPreferences.Editor editor;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment_blank, container, false);

        budget_btn = view.findViewById(R.id.budget_btn);
        usehelp_btn = view.findViewById(R.id.usehelp_btn);
        about_btn = view.findViewById(R.id.about_btn);
        setting_btn = view.findViewById(R.id.setting_btn);
        signup = view.findViewById(R.id.signup);
        clock_day = view.findViewById(R.id.clock_day);

        budget_btn.setOnClickListener(this);
        usehelp_btn.setOnClickListener(this);
        about_btn.setOnClickListener(this);
        setting_btn.setOnClickListener(this);
        signup.setOnClickListener(this);
        
        //设置累计签到的天数
        sp =getActivity().getSharedPreferences("tice", Context.MODE_PRIVATE);
        clock_day.setText(sp.getString("tice","0"));

        return view;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.signup://签到
                setSignup();
                break;
            case R.id.budget_btn://预算中心
                Intent intent2 = new Intent(getContext(),BudgetActivity.class);
                startActivity(intent2);
                break;
            case R.id.usehelp_btn://使用帮助
                Intent intent3 = new Intent(getContext(),UsehelpActivity.class);
                startActivity(intent3);
                break;
            case R.id.about_btn://关于我们
                Intent intent4 = new Intent(getContext(),AboutActivity.class);
                startActivity(intent4);
                break;
            case R.id.setting_btn://设置
                Intent intent5 = new Intent(getContext(),SettingActivity.class);
                startActivity(intent5);
                break;

        }
    }

    //签到功能的实现
    public void setSignup(){

        //显示签到成功视图
        write = new AlertDialog.Builder(getActivity()).create();
        sigup_view = LayoutInflater.from(getActivity()).inflate(R.layout.pop_up_signup,null);
        write.setView(sigup_view);
        write.show();

        int count = Integer.parseInt(clock_day.getText().toString())+1;

        editor = sp.edit();
        editor.putString("tice",Integer.toString(count));
        editor.commit(); //写入
        onResume();//刷新
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences sp =getActivity().getSharedPreferences("tice",Context.MODE_PRIVATE);
        clock_day.setText(sp.getString("tice","0"));
    }
}

 补充说明:注意配置AndroidManifest.xml文件





3.每日签到功能的实现

自定义弹窗布局文件:pop_up_signup.xml,用于显示签到成功




    

        

        

    

累计签到的天数,采用的是SharedPreferences存储,一开始先获取里面的值,然后设置控件id:clock_day的值。

//设置累计签到的天数 
    sp =getActivity().getSharedPreferences("tice", Context.MODE_PRIVATE);                 
    clock_day.setText(sp.getString("tice","0"));

当点击 每日签到 时,跳出弹窗提示签到成功,同时获取当前累计签到的天数值并加1,然后将结果写入SharedPreferences中,并刷新。

 //签到功能的实现
    public void setSignup(){

        //显示签到成功视图
        write = new AlertDialog.Builder(getActivity()).create();
        sigup_view = LayoutInflater.from(getActivity()).inflate(R.layout.pop_up_signup,null);
        write.setView(sigup_view);
        write.show();

        int count = Integer.parseInt(clock_day.getText().toString())+1;

        editor = sp.edit();
        editor.putString("tice",Integer.toString(count));
        editor.commit(); //写入
        onResume();//刷新
    }

4.实现效果

[Android Studio] 个人主页界面的实现_第1张图片

5.图片素材

你可能感兴趣的:(Android,Studio,android,studio,android)