ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

个人界面(5分钟教学)

2021-12-22 20:58:01  阅读:118  来源: 互联网

标签:界面 void private 分钟 token Override new 教学 public


//个人界面的样式表
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.subway.SubPersonFragment"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/back"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/ic_back_s"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <LinearLayout
            android:layout_width="180dp"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="个人中心"
                android:textSize="25dp"
                android:layout_marginTop="15dp"
                android:layout_marginLeft="70dp"/>

        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#c7c7c7"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/person_head_img"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:background="@drawable/ic_head_img"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/person_nickname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="立即登录"
                android:textSize="20dp"
                android:layout_marginTop="20dp"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/username_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="账号:"
                    android:layout_marginTop="10dp"
                    android:visibility="gone"/>

                <TextView
                    android:id="@+id/person_username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="111"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="10dp"
                    android:visibility="gone"/>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="栏目"
        android:layout_marginLeft="10dp"/>

    <ListView
        android:id="@+id/subway_person_column"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:layout_marginTop="10dp"/>
</LinearLayout>
//返回个人界面的fragment(在创建几个需要跳转的Activity)

public class PersonFragment extends Fragment {
    private ImageView mPersonHeadImg;
    private TextView mPersonNickname;
    private TextView mUsernameTitle;
    private TextView mPersonUsername;
    private ListView mPersonInfo;
    private Button mPersonBtnExit;
    private PersonInfo personInfo1;
    private PersonInfo personInfo2;
    private PersonInfo personInfo3;
    private PersonInfo personInfo4;
    private String token;
    GetPersonInfo json;

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case GET_INFO_WHAT:
                    json = (GetPersonInfo) msg.obj;
                    showInfo();
                    break;
            }
        }
    };

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, container, false);
        mPersonHeadImg = (ImageView) view.findViewById(R.id.person_head_img);
        mPersonNickname = (TextView) view.findViewById(R.id.person_nickname);
        mUsernameTitle = (TextView) view.findViewById(R.id.username_title);
        mPersonUsername = (TextView) view.findViewById(R.id.person_username);
        mPersonInfo = (ListView) view.findViewById(R.id.person_info);
        mPersonBtnExit = (Button) view.findViewById(R.id.person_btn_exit);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        loadPersonInfo();
        mPersonNickname.setOnClickListener(new NickNameClick());
        mPersonInfo.setOnItemClickListener(new PersonInfoItemClick());
        mPersonBtnExit.setOnClickListener(new PersonBrnExitClick());
    }

    @Override
    public void onResume() {
        super.onResume();
        token = CacheUtils.getString(getContext(), TOKEN);//取出token
        if (token != null){
            new initHomeHttp(handler).getInfoData(getContext(),token);//初始化用户信息
        }
    }

    //退出登录
    class PersonBrnExitClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            CacheUtils.clearString(getContext(),TOKEN);
            CacheUtils.clearString(getContext(),USER_INFO);
            mPersonBtnExit.setVisibility(View.GONE);
            mPersonNickname.setText("立即登录");
            mUsernameTitle.setVisibility(View.GONE);
            mPersonUsername.setText("");
            Toast.makeText(getContext(),"退出成功!",Toast.LENGTH_SHORT).show();
        }
    }

    //条目点击跳转
    class PersonInfoItemClick implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position){
                case 0:
                    Intent intentInfo = new Intent(getContext(), InfoActivity.class);
                    startActivity(intentInfo);
                    break;
                case 1:
                    Intent intentOrder = new Intent(getContext(), EmptyActivity.class);
                    intentOrder.putExtra(VALUES,"订单列表");
                    startActivity(intentOrder);
                    break;
                case 2:
                    Intent intentPwd = new Intent(getContext(), ModifyPwdActivity.class);
                    startActivity(intentPwd);
                    break;
                case 3:
                    Intent intentSuggestion = new Intent(getContext(), SuggestionActivity.class);
                    startActivity(intentSuggestion);
                    break;
            }
        }
    }


    //显示信息
    public void showInfo() {
        mPersonNickname.setText(json.getUser().getNickName());
        mUsernameTitle.setVisibility(View.VISIBLE);
        mPersonUsername.setVisibility(View.VISIBLE);
        mPersonUsername.setText(json.getUser().getUserName());
        mPersonBtnExit.setVisibility(View.VISIBLE);
    }

    //跳转到登录界面
    class NickNameClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), LoginActivity.class);
            startActivity(intent);
        }
    }

    //设置个人中心列表
    public void loadPersonInfo() {
        //假数据
        personInfo1 = new PersonInfo();
        personInfo2 = new PersonInfo();
        personInfo3 = new PersonInfo();
        personInfo4 = new PersonInfo();

        personInfo1.setImage(R.drawable.ic_person_info);
        personInfo1.setName("个人信息");
        personInfo1.setBackImg(R.drawable.ic_to_icon);
        personInfo2.setImage(R.drawable.ic_person_info);
        personInfo2.setName("订单列表");
        personInfo2.setBackImg(R.drawable.ic_to_icon);
        personInfo3.setImage(R.drawable.ic_person_info);
        personInfo3.setName("修改密码");
        personInfo3.setBackImg(R.drawable.ic_to_icon);
        personInfo4.setImage(R.drawable.ic_person_info);
        personInfo4.setName("意见反馈");
        personInfo4.setBackImg(R.drawable.ic_to_icon);

        ArrayList<PersonInfo> personInfos = new ArrayList<>();
        personInfos.add(personInfo1);
        personInfos.add(personInfo2);
        personInfos.add(personInfo3);
        personInfos.add(personInfo4);

        SuperBase superBase = new SuperBase(personInfos.size()) {
            private ImageView mImg;
            private TextView mPersonInfoTitle;
            private ImageView mToLogin;

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_person_info, parent, false);
                mImg = (ImageView) convertView.findViewById(R.id.img);
                mPersonInfoTitle = (TextView) convertView.findViewById(R.id.person_info_title);
                mToLogin = (ImageView) convertView.findViewById(R.id.to_login);
                mImg.setBackgroundResource(personInfos.get(position).getImage());
                mPersonInfoTitle.setText(personInfos.get(position).getName());
                mToLogin.setBackgroundResource(personInfos.get(position).getBackImg());
                return convertView;
            }
        };
        mPersonInfo.setAdapter(superBase);
    }


}
3:去登录的bean类

public class MCallBack {
    /**
     * {
     *   "msg": "操作成功",
     *   "code": 200,
     *   "token": "eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjExZmU5NGNjLWE5ZmMtNDlkYS04ODZjLTA0NTUzNDVmMjg2MiJ9.mNmAngrSwiIgoINMZiEmHnNTyapn4JeLS6P8-J3t77peNJlmjnYoODfyWnxidQUxNOTaHAeIEKBcItjB9YVsXQ"
     * }
     * */

    private String msg;
    private int code;
    private String token;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }
}
4:去登录的Activity中

public class LoginActivity extends AppCompatActivity {
    private EditText mEdtUsername;
    private EditText mEdtPwd;
    private TextView mRegJudge;
    Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOGIN_WHAT:
                    MCallBack mCallBack = (MCallBack) msg.obj;
                    if (mCallBack.getCode() == 200) {
                        Toast.makeText(LoginActivity.this, "登录成功!", Toast.LENGTH_SHORT).show();
                        CacheUtils.putString(LoginActivity.this, TOKEN, mCallBack.getToken());//存token
                       finish();
                    } else {
                        Toast.makeText(LoginActivity.this, mCallBack.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    //异步请求用户登录数据
    public void loginData() {
        String form = "{" +
                "\"username\":\""+mEdtUsername.getText().toString()+"\"," +
                "\"password\":\""+mEdtPwd.getText().toString()+"\"" +
                "}";
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, form);
        Request request = new Request.Builder()
                .url(IP + LOGIN)
                .post(body)
                .build();
        new OkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String loginDataStr = response.body().string();
                Log.e("TAG", "onResponse: "+loginDataStr);
                MCallBack mCallBack = new Gson().fromJson(loginDataStr, MCallBack.class);
                Message message = new Message();
                message.what = LOGIN_WHAT;
                message.obj = mCallBack;
                handler.sendMessage(message);
            }
        });
    }

    public void initView() {
        mEdtUsername = (EditText) findViewById(R.id.edt_username);
        mEdtPwd = (EditText) findViewById(R.id.edt_pwd);
        mRegJudge = (TextView) findViewById(R.id.reg_judge);
    }

    //点击返回
    public void backPerson(View view) {
        finish();
    }

    //点击登录
    public void login(View view) {
        loginData();
    }
}
5:个人信息的样式表(activity_info)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.user.InfoActivity"
    android:orientation="vertical">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#00BCD4"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:background="@drawable/ic_back" />

        <TextView
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:text="个人信息"
            android:textSize="25dp"
            android:textColor="@color/white"
            android:gravity="center"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/info_head_img"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:background="@drawable/ic_head_img"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"/>

        <TextView
            android:id="@+id/info_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="未登录"
            android:textSize="20dp"
            android:textAlignment="center"
            android:layout_marginTop="20dp"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#3F51B5"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/info_card_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身份证:"
            android:textSize="20dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="70dp"/>

        <TextView
            android:id="@+id/info_card_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:layout_marginTop="8dp"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="30dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="昵 称:"
            android:textSize="25dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="70dp"/>

        <EditText
            android:id="@+id/info_nickname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=""
            android:background="@null"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性 别:"
            android:textSize="25dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="70dp"/>

        <EditText
            android:id="@+id/info_sex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=""
            android:background="@null"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电话号码:"
            android:textSize="25dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="70dp"/>

        <EditText
            android:id="@+id/info_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint=""
            android:background="@null"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>

    <Button
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:text="修改信息"
        android:textSize="20dp"
        android:textColor="@color/white"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:background="#00BCD4"
        android:onClick="modifyInfo"/>


  </LinearLayout>
6:回到个人界面的Activity(InfoActivity)

public class InfoActivity extends AppCompatActivity {
    private ImageView mInfoHeadImg;
    private TextView mInfoUsername;
    private TextView mInfoCardTitle;
    private TextView mInfoCardTxt;
    private EditText mInfoNickname;
    private EditText mInfoSex;
    private EditText mInfoPhone;
    private ImageView back;
    private String token;
    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MODIFY_INFO_WHAT:
                    MCallBack mCallBack = (MCallBack) msg.obj;
                    if (mCallBack.getCode()==200){
                        Toast.makeText(InfoActivity.this,"修改成功!",Toast.LENGTH_SHORT).show();
                        finish();
                    }
                    else {
                        Toast.makeText(InfoActivity.this,mCallBack.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        initView();

        //显示信息
        token = CacheUtils.getString(InfoActivity.this, TOKEN);//取出token
        if (!token.isEmpty()){
            showInfo();
        }
    }

    //异步请求修改用户信息数据
    public void modifyInfoData(){
        String sexs="";
        SexUtils.putSex(InfoActivity.this,mInfoSex.getText().toString(),sexs);
        String form = "{"+
                "\"nickName\": \""+mInfoNickname.getText().toString()+"\","+
                "\"phonenumber\": \""+mInfoPhone.getText().toString()+"\","+
                "\"sex\": \""+sexs+"\""+
                "}";
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, form);
        Request request = new Request.Builder()
                .url(IP + MODIFY_INFO)
                .put(body)
                .addHeader("authorization", token)
                .addHeader("content-type", "application/json")
                .addHeader("cache-control", "no-cache")
                .addHeader("postman-token", "06c9f262-35ae-af77-51e5-8d68a15de94b")
                .build();
        new OkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String modifyInfoDataStr = response.body().string();
                MCallBack mCallBack = new Gson().fromJson(modifyInfoDataStr, MCallBack.class);
                Message message = new Message();
                message.what = MODIFY_INFO_WHAT;
                message.obj = mCallBack;
                handler.sendMessage(message);
            }
        });
    }

    //显示信息
    public void showInfo(){
        String json = CacheUtils.getString(InfoActivity.this,USER_INFO);//取出用户信息
        GetPersonInfo getPersonInfo = new Gson().fromJson(json, GetPersonInfo.class);//在解析一遍

        mInfoUsername.setText(getPersonInfo.getUser().getUserName());

        //身份证
        String cardId = getPersonInfo.getUser().getIdCard().substring(0,2)+"************"+getPersonInfo.getUser().getIdCard().substring(14);
        mInfoCardTxt.setText(cardId);

        mInfoNickname.setHint(getPersonInfo.getUser().getNickName());

        //性别
        mInfoSex.setHint(getPersonInfo.getUser().getSex());



        //电话
        mInfoPhone.setHint(getPersonInfo.getUser().getPhonenumber());
    }

    //初始化
    public void initView(){
        mInfoHeadImg = (ImageView) findViewById(R.id.info_head_img);
        mInfoUsername = (TextView) findViewById(R.id.info_username);
        mInfoCardTitle = (TextView) findViewById(R.id.info_card_title);
        mInfoCardTxt = (TextView) findViewById(R.id.info_card_txt);
        mInfoNickname = (EditText) findViewById(R.id.info_nickname);
        mInfoSex = (EditText) findViewById(R.id.info_sex);
        mInfoPhone = (EditText) findViewById(R.id.info_phone);
        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //修改信息
    public void modifyInfo(View view) {
        modifyInfoData();
    }
}
7:订单列表的样式表(activity_empty)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.EmptyActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/back"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/ic_back_s"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <TextView
            android:id="@+id/title"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:text="title"
            android:textSize="30dp"
            android:gravity="center"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#c7c7c7"/>

    <TextView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Content"
        android:gravity="center"
        android:textSize="30dp"/>


</LinearLayout>
8:回到订单列表(EmptyActivity)

public class EmptyActivity extends AppCompatActivity {
    private ImageView mBack;
    private TextView mContent;
    private TextView mTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty);
        initView();
        String values = getIntent().getStringExtra(VALUES);//取值
        mContent.setText(values);
        mTitle.setText(values);
    }

    public void initView(){
        mBack = (ImageView) findViewById(R.id.back);
        mContent = (TextView) findViewById(R.id.content);
        mTitle = (TextView) findViewById(R.id.title);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
9:修改密码的样式表(activity_modiy_pwd)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.user.ModifyPwdActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#00BCD4"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/back"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/ic_back"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <TextView
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:text="设置密码"
            android:textSize="30dp"
            android:textColor="@color/white"
            android:gravity="center"/>
    </LinearLayout>


    <EditText
        android:id="@+id/new_pwd"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:background="@drawable/bg_login"
        android:hint="请输入新密码"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:inputType="textPassword"/>

    <EditText
        android:id="@+id/old_pwd"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:background="@drawable/bg_login"
        android:hint="请输入旧密码"
        android:textAlignment="center"
        android:textColor="@color/black"
        android:layout_gravity="center"
        android:layout_marginTop="50dp"
        android:inputType="textPassword"/>

    <Button
        android:layout_width="200dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginTop="80dp"
        android:background="#00BCD4"
        android:onClick="modifyPwd"
        android:text="确  定"
        android:textColor="@color/white"
        android:textSize="20dp" />


</LinearLayout>

10:回到修改页面的Activity中(ModifyPwdActivity)

public class ModifyPwdActivity extends AppCompatActivity {
    private ImageView mBack;
    private EditText mNewPwd;
    private EditText mOldPwd;
    private String token;

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MODIFY_PWD_WHAT:
                    MCallBack mCallBack = (MCallBack) msg.obj;
                    if (mCallBack.getCode()==200){
                        Toast.makeText(ModifyPwdActivity.this,"修改成功!请重新登录",Toast.LENGTH_SHORT).show();
                        CacheUtils.clearString(ModifyPwdActivity.this,TOKEN);
                        CacheUtils.clearString(ModifyPwdActivity.this,USER_INFO);
                        Intent intent = new Intent(ModifyPwdActivity.this,LoginActivity.class);
                        startActivity(intent);
                    }
                    else {
                        Toast.makeText(ModifyPwdActivity.this,mCallBack.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modiy_pwd);
        initView();
    }

    public void initView(){
        mBack = (ImageView) findViewById(R.id.back);
        mNewPwd = (EditText) findViewById(R.id.new_pwd);
        mOldPwd = (EditText) findViewById(R.id.old_pwd);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //异步请求修改密码数据
    public void modifyPwdData(){
        String form = "{"+
                "\"newPassword\": \""+mNewPwd.getText().toString()+"\","+
                "\"oldPassword\": \""+mOldPwd.getText().toString()+"\""+
                "}";
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, form);
        Request request = new Request.Builder()
                .url(IP + MODIFY_PWD)
                .put(body)
                .addHeader("authorization", token)
                .addHeader("content-type", "application/json")
                .addHeader("cache-control", "no-cache")
                .addHeader("postman-token", "3464858b-4b35-2bd6-5b78-c5c81b6f302e")
                .build();
        new OkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String modifyPwdDataStr = response.body().string();
                MCallBack mCallBack = new Gson().fromJson(modifyPwdDataStr, MCallBack.class);
                Message message = new Message();
                message.what = MODIFY_PWD_WHAT;
                message.obj = mCallBack;
                handler.sendMessage(message);
            }
        });
    }

    //修改密码
    public void modifyPwd(View view) {
        //获取token
        token = CacheUtils.getString(ModifyPwdActivity.this, TOKEN);
        if (!token.isEmpty()){
            modifyPwdData();
        }
    }
}
11:意见反馈样式表(activity_suggestion)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.user.SuggestionActivity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#00BCD4"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/back"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/ic_back"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"/>

        <TextView
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:text="意见反馈"
            android:textSize="30dp"
            android:textColor="@color/white"
            android:gravity="center"/>
    </LinearLayout>

    <EditText
        android:id="@+id/edt_suggestion"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:maxLength="150"
        android:hint="说说你想说的" />

    <Button
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_marginLeft="230dp"
        android:textColor="@color/white"
        android:text="提 交"
        android:background="#00BCD4"
        android:textSize="20dp"
        android:layout_marginTop="20dp"
        android:onClick="submit"/>

</LinearLayout>
12:意见反馈(SuggestionActivity)

public class SuggestionActivity extends AppCompatActivity {
    private ImageView mBack;
    private EditText mEdtSuggestion;
    private String token;

    Handler handler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case SUGGESTION_WHAT:
                    MCallBack mCallBack = (MCallBack) msg.obj;
                    if (mCallBack.getCode()==200){
                        Toast.makeText(SuggestionActivity.this,"提交成功!",Toast.LENGTH_SHORT).show();
                        mEdtSuggestion.setText("");
                    }
                    else {
                        Toast.makeText(SuggestionActivity.this,mCallBack.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggestion);
        initView();
    }

    //异步请求反馈数据
    public void suggestionData(){
        String form = "{\n" +
                "\"content\": \""+mEdtSuggestion.getText().toString()+"\",\n" +
                "\"title\": \"反馈内容\"\n" +
                "}";
        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, form);
        Request request = new Request.Builder()
                .url(IP + SUGGESTION)
                .post(body)
                .addHeader("authorization", token)
                .addHeader("content-type", "application/json")
                .addHeader("cache-control", "no-cache")
                .addHeader("postman-token", "55d63e36-1045-9069-6761-26cfe7e5fc45")
                .build();
        new OkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String suggestionDataStr = response.body().string();
                MCallBack mCallBack = new Gson().fromJson(suggestionDataStr, MCallBack.class);
                Message message = new Message();
                message.what = SUGGESTION_WHAT;
                message.obj = mCallBack;
                handler.sendMessage(message);
            }
        });
    }

    public void initView(){
        mBack = (ImageView) findViewById(R.id.back);
        mEdtSuggestion = (EditText) findViewById(R.id.edt_suggestion);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //提交反馈意见
    public void submit(View view) {
        token = CacheUtils.getString(SuggestionActivity.this, TOKEN);
        if (!token.isEmpty()){
            suggestionData();
        }
    }
}

标签:界面,void,private,分钟,token,Override,new,教学,public
来源: https://blog.csdn.net/m0_52625686/article/details/122094429

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有