冲刺第十五天

PersonalInformationActivity.java
  1 package com.example.lileme.ThreeFragment.MyselfDetile;
  2 
  3 import android.content.DialogInterface;
  4 import android.os.Bundle;
  5 import android.support.v7.app.AlertDialog;
  6 import android.support.v7.app.AppCompatActivity;
  7 import android.view.View;
  8 import android.widget.ImageView;
  9 import android.widget.LinearLayout;
 10 import android.widget.TextView;
 11 
 12 import com.example.lileme.R;
 13 import com.example.lileme.Service.GetPersonalInfoService;
 14 import com.example.lileme.Service.SetPersonalInfoService;
 15 import com.example.lileme.Util.Toastutil;
 16 
 17 import java.util.ArrayList;
 18 
 19 public class PersonalInformationActivity extends AppCompatActivity {
 20     private ImageView mIvback;                          //返回键
 21     private TextView mTvtitle;                          //标题
 22     private LinearLayout mLl_3;                       //获取姓名所在布局
 23     private LinearLayout mLl_4;                       //获取手机号所在布局
 24     private LinearLayout mLl_5;                       //获取性别所在布局
 25     private TextView mTv_name_answer;          //获取姓名两个TextView
 26     private TextView mTv_phone_answer;         //获取手机号两个TextView
 27     private TextView mTv_sex_answer;           //获取性别两个TextView
 28                 //其他两个不可修改
 29     private String user;//接收user
 30     private String myname;                      //个人信息设置时文本框输入的姓名
 31     @Override
 32     protected void onCreate(Bundle savedInstanceState) {
 33         super.onCreate(savedInstanceState);
 34         setContentView(R.layout.activity_porsonal_information);
 35         mIvback = findViewById(R.id.iv_back);
 36         mTvtitle = findViewById(R.id.tv_title);
 37         mLl_3 = findViewById(R.id.ll_3);               //获取姓名所在布局
 38         mLl_4 = findViewById(R.id.ll_4);               //获取手机号所在布局
 39         mLl_5 = findViewById(R.id.ll_5);               //获取性别所在布局
 40         mTv_name_answer = findViewById(R.id.tv_name_answer);//获取姓名id
 41         mTv_phone_answer = findViewById(R.id.tv_phone_answer);//获取手机号id
 42         mTv_sex_answer = findViewById(R.id.tv_sex_answer);//获取性别id
 43         Thread thread=new Thread(new GetPersonalInfoThread());                                       //设置初始资料(数据库获取)
 44         thread.start();
 45         try {
 46             thread.join();
 47         } catch (InterruptedException e) {
 48             e.printStackTrace();
 49         }
 50         Listener();
 51     }
 52     public void Listener(){
 53         Onclick onclick=new Onclick();
 54         mIvback.setOnClickListener(onclick);
 55         mTvtitle.setOnClickListener(onclick);
 56         mLl_3.setOnClickListener(onclick);
 57         mLl_4.setOnClickListener(onclick);
 58         mLl_5.setOnClickListener(onclick);
 59     }
 60     class Onclick implements View.OnClickListener{
 61 
 62         @Override
 63         public void onClick(View v) {
 64             switch (v.getId()){
 65                 case R.id.iv_back:                      //返回键
 66                     finish();
 67                     break;
 68                 case R.id.ll_3:                         //设置姓名
 69                     setname();
 70                     break;
 71                 case R.id.ll_4:                         //点击手机号不允许修改
 72                     Toastutil.showmsg(getApplicationContext(),"请重新注册手机号");
 73                     break;
 74                 case R.id.ll_5:
 75                     setsex();                           //设置性别
 76                     break;
 77             }
 78         }
 79     }
 80 
 81 
 82     //以后设置姓名
 83     public void setname(){
 84         UpdateNameDialog dialog = new UpdateNameDialog(PersonalInformationActivity.this);
 85         dialog.setTitle("请输入姓名").setCancle("取消", new UpdateNameDialog.IOnCancleListener() {
 86             @Override
 87             public void OnCancle(UpdateNameDialog dialog) {
 88                 dialog.dismiss();
 89             }
 90         }).setConfirm("确定", new UpdateNameDialog.IOnConfirmListener() {
 91             @Override
 92             public void OnConfirm(UpdateNameDialog dialog) {
 93                 myname = dialog.getName();           //得到文本框内容
 94                 if(!myname.isEmpty()){
 95                     if(myname.contains(" ")){
 96                         Toastutil.showmsg(PersonalInformationActivity.this," 输入内容不含有空格");
 97                     }else{
 98                         if(!myname.equals(mTv_name_answer.getText().toString())){
 99                             mTv_name_answer.setText(myname);
100                             new Thread(new SetPersonalNameThread()).start();
101                             dialog.dismiss();
102                         }
103                         else{
104                             Toastutil.showmsg(PersonalInformationActivity.this,"与原内容相同");
105                         }
106                     }
107                 }else{
108                     Toastutil.showmsg(PersonalInformationActivity.this,"请输入姓名");
109                 }
110             }
111         }).setCanceledOnTouchOutside(false);
112        dialog.show();
113     }
114 
115 
116 
117     //以后设置性别dialog
118     public void setsex(){
119         final String[] arrays = new String[]{"男","女"};
120         AlertDialog.Builder builder = new AlertDialog.Builder(PersonalInformationActivity.this);
121         builder.setItems(arrays, new DialogInterface.OnClickListener() {
122             @Override
123             public void onClick(DialogInterface dialog, final int which) {
124                 mTv_sex_answer.setText(arrays[which]);
125                 new Thread(new Runnable() {
126                     @Override
127                     public void run() {
128                         //获取服务器返回数据
129                         SetPersonalInfoService.executeHttpPost("user_sex",arrays[which],getUser(),"SetPersonalInfo");
130                     }
131                 }).start();
132             }
133         }).show();
134 
135     }
136 
137     //数据库获取账号姓名手机号性别
138     public class GetPersonalInfoThread implements Runnable{
139         @Override
140         public void run() {
141             //获取服务器返回数据
142             ArrayList<String> list = GetPersonalInfoService.executeHttpPost(getUser(),"GetPersonalInfo");
143             mTv_name_answer.setText(list.get(0));
144             mTv_phone_answer.setText(getUser());
145             mTv_sex_answer.setText(list.get(1));
146         }
147     }
148 
149     //传递设置的个人信息姓名到数据库
150     public class SetPersonalNameThread implements Runnable{
151         @Override
152         public void run() {
153              //获取服务器返回数据
154              SetPersonalInfoService.executeHttpPost("user_name",myname,getUser(),"SetPersonalInfo");
155         }
156     }
157     //接收从MainActivity中传递过来的user
158     public String getUser(){
159         String user;
160         Bundle bundle = getIntent().getExtras();
161         if(bundle == null){
162             user = null;
163         }else{
164             user = bundle.getString("user");
165         }
166         return user;
167     }
168 }

activity_personal_information.xml

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:background="@color/dark">
  6     <LinearLayout
  7         android:id="@+id/ll_1"
  8         android:layout_width="match_parent"
  9         android:layout_height="wrap_content"
 10         android:orientation="horizontal"
 11         android:background="@color/indexcolor">
 12         <ImageView
 13             android:id="@+id/iv_back"
 14             android:layout_width="25dp"
 15             android:layout_height="30dp"
 16             android:background="@drawable/back"
 17             android:scaleY="0.8"
 18             android:layout_gravity="center_vertical"
 19             android:layout_marginTop="5dp"
 20             android:layout_marginBottom="5dp"
 21             android:layout_marginLeft="5dp"/>
 22         <LinearLayout
 23             android:layout_width="match_parent"
 24             android:layout_height="wrap_content"
 25             android:orientation="vertical">
 26             <TextView
 27                 android:id="@+id/tv_title"
 28                 android:layout_width="wrap_content"
 29                 android:layout_height="wrap_content"
 30                 android:text="个人资料"
 31                 android:textSize="20sp"
 32                 android:layout_gravity="center_horizontal"
 33                 android:layout_marginTop="5dp"
 34                 android:layout_marginBottom="5dp"
 35                 android:layout_marginRight="15dp"/>
 36         </LinearLayout>
 37     </LinearLayout>
 38 
 39 
 40     <View
 41         android:id="@+id/v1"
 42         android:layout_width="match_parent"
 43         android:layout_height="1dp"
 44         android:layout_below="@+id/ll_1"
 45         android:background="@color/dark"/>
 46     <LinearLayout
 47         android:id="@+id/ll_3"
 48         android:layout_width="match_parent"
 49         android:layout_height="50dp"
 50         android:orientation="horizontal"
 51         android:layout_below="@+id/v1"
 52         android:background="#fff">
 53         <TextView
 54             android:id="@+id/tv_name"
 55             android:layout_width="100dp"
 56             android:layout_height="wrap_content"
 57             android:text="姓名"
 58             android:textSize="20sp"
 59             android:layout_marginLeft="20dp"
 60             android:layout_gravity="center_vertical"
 61             android:textColor="#000"/>
 62         <TextView
 63             android:id="@+id/tv_name_answer"
 64             android:layout_width="match_parent"
 65             android:layout_height="wrap_content"
 66             android:text="张旺"
 67             android:maxLines="1"
 68             android:textSize="20sp"
 69             android:drawableRight="@drawable/dayuhao"
 70             android:drawablePadding="10dp"
 71             android:layout_gravity="center_vertical"
 72             android:layout_marginRight="10dp"
 73             android:gravity="right"
 74             android:maxLength="4"/>
 75     </LinearLayout>
 76     <View
 77         android:id="@+id/v2"
 78         android:layout_width="match_parent"
 79         android:layout_height="1dp"
 80         android:layout_below="@+id/ll_3"
 81         android:background="@color/dark"/>
 82     <LinearLayout
 83         android:id="@+id/ll_4"
 84         android:layout_width="match_parent"
 85         android:layout_height="50dp"
 86         android:orientation="horizontal"
 87         android:layout_below="@+id/v2"
 88         android:background="#fff">
 89         <TextView
 90             android:id="@+id/tv_phone"
 91             android:layout_width="100dp"
 92             android:layout_height="wrap_content"
 93             android:text="手机号"
 94             android:textSize="20sp"
 95             android:layout_marginLeft="20dp"
 96             android:layout_gravity="center_vertical"
 97             android:textColor="#000"/>
 98         <TextView
 99             android:id="@+id/tv_phone_answer"
100             android:layout_width="match_parent"
101             android:layout_height="wrap_content"
102             android:text="18203232573"
103             android:maxLines="1"
104             android:textSize="20sp"
105             android:drawablePadding="10dp"
106             android:layout_gravity="center_vertical"
107             android:layout_marginRight="10dp"
108             android:gravity="right"/>
109     </LinearLayout>
110     <View
111         android:id="@+id/v3"
112         android:layout_width="match_parent"
113         android:layout_height="1dp"
114         android:layout_below="@+id/ll_4"
115         android:background="@color/dark"/>
116     <LinearLayout
117         android:id="@+id/ll_5"
118         android:layout_width="match_parent"
119         android:layout_height="50dp"
120         android:orientation="horizontal"
121         android:layout_below="@+id/v3"
122         android:background="#fff">
123         <TextView
124             android:id="@+id/tv_sex"
125             android:layout_width="100dp"
126             android:layout_height="wrap_content"
127             android:text="性别"
128             android:textSize="20sp"
129             android:layout_marginLeft="20dp"
130             android:layout_gravity="center_vertical"
131             android:textColor="#000"/>
132         <TextView
133             android:id="@+id/tv_sex_answer"
134             android:layout_width="match_parent"
135             android:layout_height="wrap_content"
136             android:text="男"
137             android:maxLines="1"
138             android:textSize="20sp"
139             android:drawableRight="@drawable/dayuhao"
140             android:drawablePadding="10dp"
141             android:layout_gravity="center_vertical"
142             android:layout_marginRight="10dp"
143             android:gravity="right"/>
144     </LinearLayout>
145 </RelativeLayout>
原文地址:https://www.cnblogs.com/zhangzhongkun/p/11060891.html