日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android中私有方法 继承,Android项目实战系列—基于博学谷(五)个人资料

發布時間:2023/12/15 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android中私有方法 继承,Android项目实战系列—基于博学谷(五)个人资料 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

由于這個模塊內容較多,篇幅較長,請耐心閱讀。

個人資料模塊分為兩個部分

[x] [個人資料]()

[x] [資料修改]()

一、個人資料

1、個人資料界面

(1)、創建個人資料界面

在com.buxuegu.activity包中創建一個java類,命名為UserInfoActivity。在res/layout文件夾下創建一個布局文件,命名為activity_user_info。

(2)、界面代碼——activity_user_info.xml

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@android:color/white">

android:id="@+id/rl_head"

android:layout_width="fill_parent"

android:layout_height="60dp"

android:layout_marginLeft="15dp"

android:layout_marginRight="15dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:text="頭像"

android:textColor="#000000"

android:textSize="16sp" />

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"/>

android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#E4E4E4"/>

android:id="@+id/rl_account"

android:layout_width="fill_parent"

android:layout_height="60dp"

android:layout_marginLeft="15dp"

android:layout_marginRight="15dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:text="用戶名"

android:textColor="#000000"

android:textSize="16sp"/>

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"/>

android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#E4E4E4"/>

android:id="@+id/rl_nickName"

android:layout_width="fill_parent"

android:layout_height="60dp"

android:layout_marginLeft="15dp"

android:layout_marginRight="15dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:text="昵稱"

android:textColor="#000000"

android:textSize="16sp"/>

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="昵稱"

android:textColor="#a3a3a3"

android:textSize="14sp"/>

android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#E4E4E4"/>

android:id="@+id/rl_sex"

android:layout_width="fill_parent"

android:layout_height="60dp"

android:layout_marginLeft="15dp"

android:layout_marginRight="15dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:text="性別"

android:textColor="#000000"

android:textSize="16sp"/>

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="女"

android:textColor="#a3a3a3"

android:textSize="14sp"/>

android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#E4E4E4"/>

android:id="@+id/rl_signature"

android:layout_width="fill_parent"

android:layout_height="60dp"

android:layout_marginLeft="15dp"

android:layout_marginRight="15dp">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:singleLine="true"

android:text="簽名"

android:textColor="#000000"

android:textSize="16sp"/>

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"/>

android:layout_width="fill_parent"

android:layout_height="1dp"

android:background="#E4E4E4"/>

2、創建UserBean

選中com.boxuegu包,新建一個bean包。在bean包中創建一個Java類,命名為UserBean。代碼如下

package com.boxuegu.bean;

public class UserBean {

public String userName; //用戶名

public String nickName; //昵稱

public String sex; //性別

public String signature; //簽名

}

3、創建用戶信息表

選中com.boxuegu包,新建一個sqlite包。在sqlite包中創建一個Java類,命名為QLiteHelper。因為SQLiteHelper類繼承SQLiteOpenHelper類,需要修改主類。代碼如下

package com.boxuegu.sqlite;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

//SQLiteHelper類繼承自SQLiteOpenHelper類

public class SQLiteHelper extends SQLiteOpenHelper {

public static final int DB_VERSION = 1; //數據庫的版本

public static final String DB_NAME = "wordpress.db"; //數據庫的名稱

public static final String U_USERINFO = "userinfo"; //個人資料

public SQLiteHelper(Context context){

super(context,DB_NAME,null,DB_VERSION);

}

//創建數據庫

@Override

public void onCreate(SQLiteDatabase db){

//創建用戶信息表

db.execSQL("CREATE TABLE IF NOT EXISTS " + U_USERINFO + "( " +

"_id INTEGER PRIMARY KEY AUTOINCREMENT, "

+ "userName VARCHAR," //用戶名

+ "nickName VARCHAR," //昵稱

+ "sex VARCHAR," //性別

+ "signature VARCHAR" //簽名

+ ")" );

}

//數據庫升級 版本號增加 升級調用此方法

@Override

public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){

db.execSQL("DROP TABLE IF NOT EXISTS " + U_USERINFO);

onCreate(db);

}

}

4、創建DBUtils工具類

選中com.boxuegu.utils包,創建一個Java類,命名為BUtilsr。用于操作數據庫。代碼如下

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();可寫的數據庫對象

db = helper.getWritableDatabase();

}

public static DBUtils getInstance(Context context) {

if (instance == null) {

instance = new DBUtils(context);

}

return instance;

}

//保存用戶個人資料信息

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);

}

//獲取個人資料信息

public UserBean getUserInfo(String userName) {

//獲取對應用戶名的個人信息

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;

}

//修改資料

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、個人資料界面邏輯代碼——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);

//設置界面為豎屏

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

spUserName = AnalysisUtils.readLoginUserName(this);

init();

initData();

setListener();

}

//初始化控件

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("個人資料");

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);

}

//從數據庫中獲取數據

private void initData(){

UserBean bean = null;

bean = DBUtils.getInstance(this).getUserInfo(spUserName);

//首先判斷一下數據庫中是否有數據

if(bean ==null){

bean = new UserBean();

bean.userName = spUserName; //用戶名不可以修改

bean.nickName = "這個是你的昵稱";

//默認為男

bean.sex = "男";

bean.signature = "這個是你的簽名";

//保存用戶信息到數據庫

DBUtils.getInstance(this).saveUserInfo(bean);

}

setValue(bean);

}

//為界面控件設置值

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);

}

//設置界面的點擊監聽事件

private void setListener() {

tv_back.setOnClickListener(this);

rl_nickName.setOnClickListener(this);

rl_sex.setOnClickListener(this);

rl_signature.setOnClickListener(this);

}

//控件的點擊事件

@Override

public void onClick(View v) {

switch (v.getId()){

//返回鍵的點擊事件

case R.id.tv_back:

this.finish();

break;

//昵稱的點擊事件

case R.id.rl_nickName:

break;

//性別的點擊事件

case R.id.rl_sex:

String sex = tv_sex.getText().toString();

sexDialog(sex);

break;

//簽名的點擊事件

case R.id.rl_signature:

break;

default:

break;

}

}

//修改性別的彈出框

private void sexDialog(String sex) {

int sexFlag = 0;

if("男".equals(sex)){

sexFlag = 0;

}else if("女".equals(sex)){

sexFlag = 1;

}

final String items[] = {"男","女"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("性別"); //設置標題

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();

}

//更新界面上的性別數據

private void setSex(String sex) {

tv_sex.setText(sex);

//更新數據庫中的性別數據

DBUtils.getInstance(this).updateUserInfo("sex",sex,spUserName);

}

}

6、修改我的界面代碼

個人資料界面是通過點擊用戶頭像進行跳轉的,找到MyInfoView.java文件的initView()方法,在注釋//已登錄跳轉到個人資料界面下方添加如下代碼:

Intent intent = new Intent(mContext, UserInfoActivity.class);

mContext.startActivity(intent);

二、資料修改界面

1、個人資料修改界面

(1)、創建個人資料修改界面

在com.boxuegu.activity包中創建一個Java類。命名為ChangeUserInfoActivity。在res/layout文件夾下面創建布局文件,命名為activity_change_user_info。

(2)、導入界面圖片

將所需界面圖片info_delete.png導入到drawable文件夾中。

(3)、資料修改界面代碼——activity_change_user_info.xml

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#eeeeee">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="center_vertical"

android:orientation="horizontal">

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"/>

android:id="@+id/iv_delete"

android:layout_width="27dp"

android:layout_height="27dp"

android:layout_marginLeft="-40dp"

android:src="@drawable/info_delete"/>

2、資料修改界面邏輯代碼——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時表示修改昵稱,為2時表示修改簽名

private EditText et_content;

private ImageView iv_delete;

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_change_user_info);

//設置界面為豎屏

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

init();

}

private void init(){

//從個人資料界面傳遞過來的標題和內容

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,"保存成功",Toast.LENGTH_SHORT).show();

ChangeUserInfoActivity.this.finish();

}else {

Toast.makeText(ChangeUserInfoActivity.this,"昵稱不能為空",Toast.LENGTH_SHORT).show();

}

break;

case 2:

if (!TextUtils.isEmpty(etContent)){

data.putExtra("signature",etContent);

setResult(RESULT_OK,data);

Toast.makeText(ChangeUserInfoActivity.this,"保存成功",Toast.LENGTH_SHORT).show();

ChangeUserInfoActivity.this.finish();

}else {

Toast.makeText(ChangeUserInfoActivity.this,"簽名不能為空",Toast.LENGTH_SHORT).show();

}

break;

}

}

});

}

//監聽個人資料修改界面輸入的文字

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){ //輸入的文本的長度

iv_delete.setVisibility(View.VISIBLE);

}else {

iv_delete.setVisibility(View.GONE);

}

switch (flag){

case 1: //1代表修改昵稱

if (len>8){ //昵稱最多8個文字,超過的部分需要進行截取

int selEndIndex = Selection.getSelectionEnd(editable);

String str = editable.toString();

//截取新字符串

String newStr = str.substring(0,8);

et_content.setText(newStr);

editable = et_content.getText();

//新字符串的長度

int newLen = editable.length();

//舊光標位置超過新字符串的位置

if (selEndIndex>newLen){

selEndIndex = editable.length();

}

//設置新光標所在位置

Selection.setSelection(editable,selEndIndex);

}

break;

case 2: //2代表修改簽名

if (len>16){ //昵稱最多16個文字,超過的部分需要進行截取

int selEndIndex = Selection.getSelectionEnd(editable);

String str = editable.toString();

//截取新字符串

String newStr = str.substring(0,16);

et_content.setText(newStr);

editable = et_content.getText();

//新字符串的長度

int newLen = editable.length();

//舊光標位置超過新字符串的位置

if (selEndIndex>newLen){

selEndIndex = editable.length();

}

//設置新光標所在位置

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、修改個人資料界面代碼

(1)、找到UserInfoActivity.java文件,在 private String spUserName;后面添加以下代碼:

private static final int CHANGE_NICKNAME = 1;//修改昵稱的自定義常量

private static final int CHANGE_SIGNATURE = 2;//修改簽名的自定義常量

(2)、找到UserInfoActivity.java文件,創建一個enterActivityForResult方法用來自定義跳轉

//自定義跳轉方法

private void enterActivityForResult(Class> to,int requestCode,Bundle b){

Intent i = new Intent(this,to); //to標識需要跳轉到的界面

i.putExtras(b); //b表示跳轉時傳遞的參數

startActivityForResult(i,requestCode); //requestCode表示一個請求碼

}

(3)、找到UserInfoActivity.java文件,找到onClick()方法,在注釋//昵稱的點擊事件下方添加如下代碼:

String name = tv_nickName.getText().toString();//獲取昵稱控件上的數據

Bundle bdName = new Bundle();

bdName.putString("content",name); //傳遞界面上的昵稱數據

bdName.putString("title","昵稱"); //傳遞界面的標題

bdName.putInt("flag",1); //flag 傳遞1表示是昵稱

//跳轉到個人資料修改界面

enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_NICKNAME,bdName);

(4)、找到UserInfoActivity.java文件,找到onClick()方法,在注釋//簽名的點擊事件下方添加如下代碼:

String signature = tv_signature.getText().toString();//獲取簽名控件上的數據

Bundle bdSignature = new Bundle();

bdSignature.putString("content",signature);//傳遞界面上的簽名數據

bdSignature.putString("title","簽名"); //傳遞界面的標題

bdSignature.putInt("flag",2);//flag 傳遞2表示是簽名

//跳轉到個人資料修改界面

enterActivityForResult(ChangeUserInfoActivity.class,CHANGE_SIGNATURE,bdSignature);

(5)、找到UserInfoActivity.java文件,在UserInfoActivity類中重寫onActivityResult()方法,添加如下代碼:

//資料修改以后回傳數據到界面

private String new_info; //最新數據

@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");//從個人資料界面回傳過來的數據

if(TextUtils.isEmpty(new_info)||new_info==null){

return;

}

tv_nickName.setText(new_info);

//更新數據庫中的呢稱字段

DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("nickName", new_info,spUserName);

}

break;

case CHANGE_SIGNATURE:

if(data!=null){

new_info = data.getStringExtra("signature");//從個人資料界面回傳過來的數據

if(TextUtils.isEmpty(new_info)||new_info==null){

return;

}

tv_signature.setText(new_info);

//更新數據庫中的簽名字段

DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("signature", new_info,spUserName);

}

break;

}

}

三、運行效果

Android項目實戰系列—基于博學谷 開源地址

???????????????

總結

以上是生活随笔為你收集整理的android中私有方法 继承,Android项目实战系列—基于博学谷(五)个人资料的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。