Please be patient to read this module because of its large content and length.
The profile module is divided into two parts
- [x] personal data
- [x] Data Modification
1. Personal Data
1. Profile interface
(1) Create a profile interface
Create a java class named UserInfoActivity in the com.buxuegu.activity package.Create a layout file named activity_user_info under the res/layout folder.
(2) Interface code - activity_user_info.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white"> <include layout="@layout/main_title_bar"/> <RelativeLayout android:id="@+id/rl_head" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="Head portrait" android:textColor="#000000" android:textSize="16sp" /> <ImageView android:id="@+id/iv_head_icon" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:src="@drawable/default_icon"/> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#E4E4E4"/> <RelativeLayout android:id="@+id/rl_account" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="User name" android:textColor="#000000" android:textSize="16sp"/> <TextView android:id="@+id/tv_user_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:text="account" android:textColor="#a3a3a3" android:textSize="14sp"/> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#E4E4E4"/> <RelativeLayout android:id="@+id/rl_nickName" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="Nickname?" android:textColor="#000000" android:textSize="16sp"/> <TextView android:id="@+id/tv_nickName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:singleLine="true" android:text="Nickname?" android:textColor="#a3a3a3" android:textSize="14sp"/> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#E4E4E4"/> <RelativeLayout android:id="@+id/rl_sex" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="Gender" android:textColor="#000000" android:textSize="16sp"/> <TextView android:id="@+id/tv_sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:text="female" android:textColor="#a3a3a3" android:textSize="14sp"/> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#E4E4E4"/> <RelativeLayout android:id="@+id/rl_signature" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:singleLine="true" android:text="autograph" android:textColor="#000000" android:textSize="16sp"/> <TextView android:id="@+id/tv_signature" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="5dp" android:textColor="#a3a3a3" android:textSize="14sp"/> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="1dp" android:background="#E4E4E4"/> </LinearLayout>
2. Create UserBean
Select the com.boxuegu package to create a new bean package.Create a Java class named UserBean in the bean package.The code is as follows
package com.boxuegu.bean; public class UserBean { public String userName; //User name public String nickName; //Nickname? public String sex; //Gender public String signature; //autograph }
3. Create User Information Table
Select the com.boxuegu package to create a new sqlite package.Create a Java class named QLiteHelper in the sqlite package.Since the SQLiteHelper class inherits the SQLiteOpenHelper class, the main class needs to be modified.The code is as follows
package com.boxuegu.sqlite; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; //The SQLiteHelper class inherits from the SQLiteOpenHelper class public class SQLiteHelper extends SQLiteOpenHelper { public static final int DB_VERSION = 1; //Version of the database public static final String DB_NAME = "wordpress.db"; //Name of the database public static final String U_USERINFO = "userinfo"; //personal data public SQLiteHelper(Context context){ super(context,DB_NAME,null,DB_VERSION); } //Create a database @Override public void onCreate(SQLiteDatabase db){ //Create User Information Table db.execSQL("CREATE TABLE IF NOT EXISTS " + U_USERINFO + "( " + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "userName VARCHAR," //User name + "nickName VARCHAR," //Nickname? + "sex VARCHAR," //Gender + "signature VARCHAR" //autograph + ")" ); } //Database Upgrade Version Number Increase Upgrade Calls This Method @Override public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){ db.execSQL("DROP TABLE IF NOT EXISTS " + U_USERINFO); onCreate(db); } }
4. Create DBUtils Tool Class
Select the com.boxuegu.utils package to create a Java class named BUtilsr.Used to manipulate databases.The code is as follows
package com.boxuegu.utils; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.boxuegu.bean.UserBean; import com.boxuegu.sqlite.SQLiteHelper; public class DBUtils { private static SQLiteHelper helper; private static SQLiteDatabase db; private static DBUtils instance = null; public DBUtils(Context context) { helper = new SQLiteHelper(context); //getWritableDatabase(); Writable database objects db = helper.getWritableDatabase(); } public static DBUtils getInstance(Context context) { if (instance == null) { instance = new DBUtils(context); } return instance; } //Save user profile information public void saveUserInfo(UserBean bean) { ContentValues cv = new ContentValues(); cv.put("userName", bean.userName); cv.put("nickName", bean.nickName); cv.put("sex", bean.sex); cv.put("signature", bean.signature); db.insert(SQLiteHelper.U_USERINFO, null, cv); } //Get profile information public UserBean getUserInfo(String userName) { //Get personal information about the corresponding user name String sql = "SELECT * FROM " + SQLiteHelper.U_USERINFO + " WHERE userName=?"; Cursor cursor = db.rawQuery(sql,new String[]{userName}); UserBean bean = null; while (cursor.moveToNext()){ bean = new UserBean(); bean.userName=cursor.getString(cursor.getColumnIndex("userName")); bean.nickName=cursor.getString(cursor.getColumnIndex("nickName")); bean.sex=cursor.getString(cursor.getColumnIndex("sex")); bean.signature=cursor.getString(cursor.getColumnIndex("signature")); } cursor.close(); return bean; } //Modify Data public void updateUserInfo(String key, String value, String userName) { ContentValues cv = new ContentValues(); cv.put(key, value); db.update(SQLiteHelper.U_USERINFO, cv, "userName = ?", new String[]{userName}); } }
5. Logical code for profile interface--UserInfoActivity.java
package com.boxuegu.activity; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ActivityInfo; import android.graphics.Color; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.os.Bundle; import android.view.View; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.boxuegu.R; import com.boxuegu.bean.UserBean; import com.boxuegu.utils.AnalysisUtils; import com.boxuegu.utils.DBUtils; public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener { private TextView tv_user_name; private TextView tv_signature; private RelativeLayout rl_signature; private TextView tv_sex; private RelativeLayout rl_sex; private TextView tv_nickName; private RelativeLayout rl_nickName; private TextView tv_back; private TextView tv_main_title; private RelativeLayout rl_title_bar; private String spUserName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_info); //Set interface to vertical screen setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); spUserName = AnalysisUtils.readLoginUserName(this); init(); initData(); setListener(); } //Initialize Control private void init() { tv_back = (TextView) findViewById(R.id.tv_back); tv_main_title = (TextView) findViewById(R.id.tv_main_title); tv_main_title.setText("personal data"); rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar); rl_title_bar.setBackgroundColor(Color.parseColor("##FF9900")); rl_nickName = (RelativeLayout) findViewById(R.id.rl_nickName); tv_nickName = (TextView) findViewById(R.id.tv_nickName); rl_sex = (RelativeLayout) findViewById(R.id.rl_sex); tv_sex = (TextView) findViewById(R.id.tv_sex); rl_signature = (RelativeLayout) findViewById(R.id.rl_signature); tv_signature = (TextView) findViewById(R.id.tv_signature); tv_user_name = (TextView) findViewById(R.id.tv_user_name); } //Getting data from a database private void initData(){ UserBean bean = null; bean = DBUtils.getInstance(this).getUserInfo(spUserName); //First determine if there is data in the database if(bean ==null){ bean = new UserBean(); bean.userName = spUserName; //User name cannot be modified bean.nickName = "This is your nickname"; //Default to male bean.sex = "male"; bean.signature = "This is your signature"; //Save user information to database DBUtils.getInstance(this).saveUserInfo(bean); } setValue(bean); } //Set values for interface controls public void setValue(UserBean bean) { tv_nickName.setText(bean.nickName); tv_sex.setText(bean.sex); tv_signature.setText(bean.signature); tv_user_name.setText(bean.userName); } //Set up click-to-listen events for the interface private void setListener() { tv_back.setOnClickListener(this); rl_nickName.setOnClickListener(this); rl_sex.setOnClickListener(this); rl_signature.setOnClickListener(this); } //Click events for controls @Override public void onClick(View v) { switch (v.getId()){ //Click Event for Return Key case R.id.tv_back: this.finish(); break; //Nickname Click Event case R.id.rl_nickName: break; //Gender Click Events case R.id.rl_sex: String sex = tv_sex.getText().toString(); sexDialog(sex); break; //Signature Click Event case R.id.rl_signature: break; default: break; } } //Modify the pop-up box for gender private void sexDialog(String sex) { int sexFlag = 0; if("male".equals(sex)){ sexFlag = 0; }else if("female".equals(sex)){ sexFlag = 1; } final String items[] = {"male","female"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Gender"); //Set Title builder.setSingleChoiceItems(items, sexFlag, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Toast.makeText(UserInfoActivity.this,items[which],Toast.LENGTH_SHORT).show();; setSex(items[which]); } }); builder.show(); } //Update gender data on the interface private void setSex(String sex) { tv_sex.setText(sex); //Update gender data in database DBUtils.getInstance(this).updateUserInfo("sex",sex,spUserName); } }
6. Modify my interface code
The profile interface is jumped by clicking on the user's avatar to find the initView() method of the MyInfoView.java file. Add the following code under the comment//logged-in jump to the profile interface:
Intent intent = new Intent(mContext, UserInfoActivity.class); mContext.startActivity(intent);
2. Data Modification Interface
1. Profile modification interface
(1) Create a profile modification interface
Create a Java class in the com.boxuegu.activity package.Name it ChangeUserInfoActivity.Create a layout file named activity_change_user_info under the res/layout folder.
(2) Import interface pictures
Import the desired interface picture info_delete.png into the drawable folder.
(3) Data modification interface code - activity_change_user_info.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eeeeee"> <include layout="@layout/main_title_bar"/> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <EditText android:id="@+id/et_content" android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:background="@android:color/white" android:gravity="center_vertical" android:paddingLeft="10dp" android:singleLine="true" android:textColor="#737373" android:textSize="14sp"/> <ImageView android:id="@+id/iv_delete" android:layout_width="27dp" android:layout_height="27dp" android:layout_marginLeft="-40dp" android:src="@drawable/info_delete"/> </LinearLayout> </LinearLayout>
2. Data Modification Interface Logic Code - ChangeUserInfoActivity.java
package com.boxuegu.activity; import android.content.Intent; import android.content.pm.ActivityInfo; import com.boxuegu.R; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.Editable; import android.text.Selection; import android.text.TextUtils; 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; public class ChangeUserInfoActivity extends AppCompatActivity { private TextView tv_main_title,tv_save; private RelativeLayout rl_title_bar; private TextView tv_back; private String title,content; private int flag; //flag 1 means modify nickname, 2 means modify signature private EditText et_content; private ImageView iv_delete; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_change_user_info); //Set interface to vertical screen setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); init(); } private void init(){ //Titles and content passed in from the profile interface title = getIntent().getStringExtra("title"); content = getIntent().getStringExtra("content"); flag = getIntent().getIntExtra("flag",0); tv_main_title = (TextView) findViewById(R.id.tv_main_title); tv_main_title.setText(title); rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar); rl_title_bar.setBackgroundColor(Color.parseColor("#FF9900")); tv_back = (TextView) findViewById(R.id.tv_back); tv_save = (TextView) findViewById(R.id.tv_save); tv_save.setVisibility(View.VISIBLE); et_content = (EditText) findViewById(R.id.et_content); iv_delete = (ImageView) findViewById(R.id.iv_delete); if (!TextUtils.isEmpty(content)){ et_content.setText(content); et_content.setSelection(content.length()); } contentListener(); tv_back.setOnClickListener(new android.view.View.OnClickListener(){ @Override public void onClick(android.view.View v){ ChangeUserInfoActivity.this.finish(); } }); iv_delete.setOnClickListener(new android.view.View.OnClickListener() { @Override public void onClick(android.view.View v) { et_content.setText(""); } }); tv_save.setOnClickListener(new android.view.View.OnClickListener() { @Override public void onClick(android.view.View view) { Intent data = new Intent(); String etContent = et_content.getText().toString().trim(); switch (flag){ case 1: if (!TextUtils.isEmpty(etContent)){ data.putExtra("nickName",etContent); setResult(RESULT_OK,data); Toast.makeText(ChangeUserInfoActivity.this,"Save Successfully",Toast.LENGTH_SHORT).show(); ChangeUserInfoActivity.this.finish(); }else { Toast.makeText(ChangeUserInfoActivity.this,"Nickname cannot be empty",Toast.LENGTH_SHORT).show(); } break; case 2: if (!TextUtils.isEmpty(etContent)){ data.putExtra("signature",etContent); setResult(RESULT_OK,data); Toast.makeText(ChangeUserInfoActivity.this,"Save Successfully",Toast.LENGTH_SHORT).show(); ChangeUserInfoActivity.this.finish(); }else { Toast.makeText(ChangeUserInfoActivity.this,"Signature cannot be empty",Toast.LENGTH_SHORT).show(); } break; } } }); } //Listen for text entered in profile modification interface public void contentListener(){ et_content.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Editable editable = et_content.getText(); int len = editable.length(); if (len>0){ //Length of input text iv_delete.setVisibility(View.VISIBLE); }else { iv_delete.setVisibility(View.GONE); } switch (flag){ case 1: //1 represents a change of nickname if (len>8){ //Nicknames have a maximum of 8 words, and more than that need to be intercepted int selEndIndex = Selection.getSelectionEnd(editable); String str = editable.toString(); //Intercept new string String newStr = str.substring(0,8); et_content.setText(newStr); editable = et_content.getText(); //Length of new string int newLen = editable.length(); //The old cursor position exceeds the position of the new string if (selEndIndex>newLen){ selEndIndex = editable.length(); } //Set the location of the new cursor Selection.setSelection(editable,selEndIndex); } break; case 2: //2 to modify signature if (len>16){ //Nicknames have a maximum of 16 words, and more than that need to be intercepted int selEndIndex = Selection.getSelectionEnd(editable); String str = editable.toString(); //Intercept new string String newStr = str.substring(0,16); et_content.setText(newStr); editable = et_content.getText(); //Length of new string int newLen = editable.length(); //The old cursor position exceeds the position of the new string if (selEndIndex>newLen){ selEndIndex = editable.length(); } //Set the location of the new cursor Selection.setSelection(editable,selEndIndex); } break; default: break; } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable arg0) { } }); } }
3. Modify the profile interface code
(1) Find the UserInfoActivity.java file and add the following code after private String spUserName:
private static final int CHANGE_NICKNAME = 1;//Modify custom constants for nicknames private static final int CHANGE_SIGNATURE = 2;//Modify custom constants for signatures
(2) Find the UserInfoActivity.java file and create an enterActivityForResult method to customize jumps
//Custom Jump Method private void enterActivityForResult(Class<?> to,int requestCode,Bundle b){ Intent i = new Intent(this,to); //To identify the interface to jump to i.putExtras(b); //b denotes the parameters passed when jumping startActivityForResult(i,requestCode); //requestCode represents a request code }
(3) Find the UserInfoActivity.java file, find the onClick() method, and add the following code below the click event for the comment//nickname:
String name = tv_nickName.getText().toString();//Get data on nickname control Bundle bdName = new Bundle(); bdName.putString("content",name); //Delivery of nickname data on the interface bdName.putString("title","Nickname?"); //Title of delivery interface bdName.putInt("flag",1); //flag pass 1 indicates a nickname //Jump to profile modification interface enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_NICKNAME,bdName);
(4) Find the UserInfoActivity.java file, find the onClick() method, and add the following code under the comment//signature click event:
String signature = tv_signature.getText().toString();//Get data on signature control Bundle bdSignature = new Bundle(); bdSignature.putString("content",signature);//Pass Signature Data on Interface bdSignature.putString("title","autograph"); //Title of delivery interface bdSignature.putInt("flag",2);//flag pass 2 means signature //Jump to profile modification interface enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_SIGNATURE,bdSignature);
(5) Find the UserInfoActivity.java file, override the onActivityResult() method in the UserInfoActivity class, and add the following code:
//Return data to interface after data modification private String new_info; //Latest data @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); switch (requestCode){ case CHANGE_NICKNAME: if(data!=null){ new_info = data.getStringExtra("nickName");//Data returned from the profile interface if(TextUtils.isEmpty(new_info)||new_info==null){ return; } tv_nickName.setText(new_info); //Update nicknamed fields in Databases DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("nickName", new_info,spUserName); } break; case CHANGE_SIGNATURE: if(data!=null){ new_info = data.getStringExtra("signature");//Data returned from the profile interface if(TextUtils.isEmpty(new_info)||new_info==null){ return; } tv_signature.setText(new_info); //Update the signature field in the database DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("signature", new_info,spUserName); } break; } }