admin
2021-05-13 4834f6ac27e483232eb0f6dae0ead378f8ea0f1a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.tejia.lijin.app.ui.mine;
 
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.app.hubert.guide.util.ScreenUtils;
import com.wpc.library.util.common.StringUtils;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.ui.BaseActivity;
import com.tejia.lijin.app.util.TopStatusSettings;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
/**
 * 修改用户昵称
 */
public class MyNickName extends BaseActivity implements View.OnClickListener {
    private TextView tv_left, tv_middle;
    private TextView tv_top_bar_right;
    private EditText mynickanme_memoName;//昵称 输入框
    private ImageView mynickanme_clear;//清除昵称
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mynickanme);
        TopStatusSettings.setStatusViewAndDeepColor(this);
        inint();
        OnClickListener();
    }
 
 
    private void inint() {
        tv_left = findViewById(R.id.tv_top_bar_left);
        tv_middle = findViewById(R.id.tv_top_bar_middle);
        tv_top_bar_right = findViewById(R.id.tv_top_bar_right);
        mynickanme_memoName = findViewById(R.id.mynickanme_memoName);
        mynickanme_clear = findViewById(R.id.mynickanme_clear);
        tv_middle.setText("修改昵称");
        tv_top_bar_right.setText("保存");
//        tv_top_bar_right.setVisibility(View.VISIBLE);
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tv_top_bar_right.getLayoutParams();
        lp.leftMargin = ScreenUtils.dp2px(this, 16);
        tv_top_bar_right.setLayoutParams(lp);
        mynickanme_memoName.setHint(getIntent().getStringExtra("name"));
    }
 
    private void OnClickListener() {
        tv_left.setOnClickListener(this);
        tv_top_bar_right.setOnClickListener(this);
        mynickanme_clear.setOnClickListener(this);
 
        mynickanme_memoName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
 
            }
 
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
 
            }
 
            @Override
            public void afterTextChanged(Editable s) {
                mynickanme_clear.setVisibility(StringUtils.isEmpty(s.toString())
                        ? View.GONE : View.VISIBLE);
            }
        });
    }
 
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_top_bar_left:
                finish();
                break;
            case R.id.tv_top_bar_right://保存昵称
                if (mynickanme_memoName.getText().toString().length() > 0) {
                    saveInfo();
                } else {
                    Toast.makeText(this, "请输入要修改的昵称", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.mynickanme_clear:
                mynickanme_memoName.getText().clear();//清除文字
                break;
            default:
                break;
        }
    }
 
    /**
     * 更换昵称
     */
    private void saveInfo() {
        final SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
        if (sp.getBoolean("isLogin", false)) {
            ShoppingApi.saveInfo(this, sp.getString("uid", "0"), mynickanme_memoName.getText().toString(), null, null, null, null, new BasicTextHttpResponseHandler() {
                @Override
                public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                    if (jsonObject.optInt("code") == 0) {//修改成功
                        Toast.makeText(MyNickName.this, "修改成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else if (jsonObject.optInt("code") == 1) {//修改失败
                        Toast.makeText(MyNickName.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
}