1.app功能:通過註冊登錄賬戶,擁有一個賬戶本,能夠將平時自己容易的忘記的賬戶記錄下來,並可以保持到雲端,不需要擔心數據丟失,只要登錄賬戶,便可獲取到自己的賬戶本。 2.實現的效果圖,如下: 以下界面分別為註冊界面、登錄界面、提交賬戶內容界面、賬戶列表界面、長按刪除賬戶信息、具體賬戶內容信息 3. ...
1.app功能:通過註冊登錄賬戶,擁有一個賬戶本,能夠將平時自己容易的忘記的賬戶記錄下來,並可以保持到雲端,不需要擔心數據丟失,只要登錄賬戶,便可獲取到自己的賬戶本。
2.實現的效果圖,如下:
以下界面分別為註冊界面、登錄界面、提交賬戶內容界面、賬戶列表界面、長按刪除賬戶信息、具體賬戶內容信息
3.實現的工程目錄如下:
4.實現的具體過程:
a.佈局代碼:
(1)登錄界面圖:activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:background="#ffffff" 5 android:weightSum="5" 6 android:orientation="vertical" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 tools:context="base.secret.MainActivity"> 10 <LinearLayout 11 android:layout_weight="2" 12 android:layout_width="match_parent" 13 android:layout_height="0dp"/> 14 <base.secret.TextInputLayout 15 android:layout_marginRight="20dp" 16 android:layout_marginLeft="20dp" 17 android:gravity="center_vertical" 18 android:id="@+id/edit_user" 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content"> 21 <EditText 22 android:textSize="20sp" 23 android:hint="@string/login_user" 24 android:gravity="center" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" /> 27 </base.secret.TextInputLayout> 28 <base.secret.TextInputLayout 29 android:layout_marginRight="20dp" 30 android:layout_marginLeft="20dp" 31 android:layout_marginTop="20dp" 32 android:id="@+id/edit_psw" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content"> 35 <EditText 36 android:inputType="textPassword" 37 android:textSize="20sp" 38 android:hint="@string/user_psw" 39 android:gravity="center" 40 android:layout_width="match_parent" 41 android:layout_height="wrap_content" /> 42 </base.secret.TextInputLayout> 43 <LinearLayout 44 android:layout_marginTop="20dp" 45 android:gravity="center" 46 android:orientation="horizontal" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content"> 49 <Button 50 android:textSize="25sp" 51 android:text="@string/login" 52 android:id="@+id/btn_login" 53 android:layout_width="wrap_content" 54 android:layout_height="wrap_content" /> 55 <Button 56 android:layout_marginLeft="20dp" 57 android:textSize="25sp" 58 android:text="@string/register" 59 android:id="@+id/btn_register" 60 android:layout_width="wrap_content" 61 android:layout_height="wrap_content" /> 62 </LinearLayout> 63 </LinearLayout>View Code
(2)註冊界面:register.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 android:weightSum="3" 4 xmlns:android="http://schemas.android.com/apk/res/android" 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 <LinearLayout 9 android:layout_weight="1" 10 android:layout_width="wrap_content" 11 android:layout_height="0dp"/> 12 <base.secret.TextInputLayout 13 android:layout_marginTop="20dp" 14 android:layout_marginRight="20dp" 15 android:layout_marginLeft="20dp" 16 android:gravity="center_vertical" 17 android:id="@+id/register_user" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content"> 20 <EditText 21 android:textSize="20sp" 22 android:hint="@string/user_name" 23 android:gravity="center" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" /> 26 </base.secret.TextInputLayout> 27 <base.secret.TextInputLayout 28 android:layout_marginRight="20dp" 29 android:layout_marginLeft="20dp" 30 android:layout_marginTop="20dp" 31 android:id="@+id/register_psw" 32 android:layout_width="match_parent" 33 android:layout_height="wrap_content"> 34 <EditText 35 android:textSize="20sp" 36 android:inputType="textPassword" 37 android:hint="@string/user_psw" 38 android:gravity="center" 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" /> 41 </base.secret.TextInputLayout> 42 <base.secret.TextInputLayout 43 android:layout_marginRight="20dp" 44 android:layout_marginLeft="20dp" 45 android:layout_marginTop="20dp" 46 android:id="@+id/register_again_psw" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content"> 49 <EditText 50 android:textSize="20sp" 51 android:hint="@string/again_psw" 52 android:inputType="textPassword" 53 android:gravity="center" 54 android:layout_width="match_parent" 55 android:layout_height="wrap_content" /> 56 </base.secret.TextInputLayout> 57 <Button 58 android:layout_marginTop="20dp" 59 android:textSize="25sp" 60 android:layout_gravity="center_horizontal" 61 android:text="@string/register" 62 android:id="@+id/btn_register2" 63 android:layout_width="wrap_content" 64 android:layout_height="wrap_content" /> 65 </LinearLayout>View Code
(3)列表界面:activity_list.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <Button 8 android:layout_gravity="center" 9 android:layout_marginTop="20dp" 10 android:textSize="20sp" 11 android:text="@string/btn_add" 12 android:id="@+id/btn_add" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" /> 15 <ListView 16 android:layout_marginTop="10dp" 17 android:id="@+id/list" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content"> 20 21 </ListView> 22 23 </LinearLayout>View Code
(4)列表中載入的佈局:item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent"> 7 <TextView 8 android:id="@+id/text_item" 9 android:layout_marginTop="20dp" 10 android:layout_marginLeft="20dp" 11 android:textSize="25sp" 12 android:layout_width="match_parent" 13 android:layout_height="wrap_content" /> 14 </LinearLayout>View Code
(5)添加賬戶頁面:activity_add.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:weightSum="3" 5 android:orientation="vertical" 6 android:layout_width="match_parent" 7 android:layout_height="match_parent"> 8 <LinearLayout 9 android:layout_weight="1" 10 android:layout_width="wrap_content" 11 android:layout_height="0dp"/> 12 <base.secret.TextInputLayout 13 android:layout_marginTop="20dp" 14 android:layout_marginRight="20dp" 15 android:layout_marginLeft="20dp" 16 android:gravity="center_vertical" 17 android:id="@+id/commit_content" 18 android:layout_width="match_parent" 19 android:layout_height="wrap_content"> 20 <EditText 21 android:textSize="20sp" 22 android:hint="@string/more_message" 23 android:gravity="center" 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" /> 26 </base.secret.TextInputLayout> 27 <base.secret.TextInputLayout 28 android:layout_marginTop="20dp" 29 android:layout_marginRight="20dp" 30 android:layout_marginLeft="20dp" 31 android:gravity="center_vertical" 32 android:id="@+id/commit_user" 33 android:layout_width="match_parent" 34 android:layout_height="wrap_content"> 35 <EditText 36 android:textSize="20sp" 37 android:hint="@string/login_user" 38 android:gravity="center" 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" /> 41 </base.secret.TextInputLayout> 42 <base.secret.TextInputLayout 43 android:layout_marginRight="20dp" 44 android:layout_marginLeft="20dp" 45 android:layout_marginTop="20dp" 46 android:id="@+id/commit_psw" 47 android:layout_width="match_parent" 48 android:layout_height="wrap_content"> 49 <EditText 50 android:textSize="20sp" 51 android:hint="@string/user_psw" 52 android:gravity="center" 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" /> 55 </base.secret.TextInputLayout> 56 <Button 57 android:layout_marginTop="20dp" 58 android:textSize="25sp" 59 android:layout_gravity="center_horizontal" 60 android:text="@string/commit" 61 android:id="@+id/btn_commit" 62 android:layout_width="wrap_content" 63 android:layout_height="wrap_content" /> 64 </LinearLayout>View Code
(6)賬戶具體信息頁面:activity_detail.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:weightSum="2" 6 android:paddingLeft="20dp" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent"> 9 <LinearLayout 10 android:layout_weight="1" 11 android:layout_width="match_parent" 12 android:layout_height="0dp"/> 13 <TextView 14 android:text="@string/user" 15 android:textSize="18sp" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" /> 18 <TextView 19 android:layout_marginTop="10dp" 20 android:id="@+id/text_username" 21 android:textSize="25sp" 22 android:layout_width="match_parent" 23 android:layout_height="wrap_content" /> 24 <TextView 25 android:layout_marginTop="20dp" 26 android:text="@string/psw" 27 android:textSize="18sp" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content" /> 30 <TextView 31 android:layout_marginTop="10dp" 32 android:textSize="30sp" 33 android:id="@+id/text_psw" 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" /> 36 </LinearLayout>View Code
b.實現代碼:
(1)主界面:MainActivity——主要是實現跳轉頁面的功能和利用BmobUser對象判斷用戶名登錄是否正確:
1 package base.secret; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.annotation.MainThread; 7 import android.support.design.widget.*; 8 import android.support.design.widget.TextInputLayout; 9 import android.view.View; 10 import android.view.Window; 11 import android.widget.Toast; 12 13 import cn.bmob.v3.Bmob; 14 import cn.bmob.v3.BmobQuery; 15 import cn.bmob.v3.BmobUser; 16 import cn.bmob.v3.exception.BmobException; 17 import cn.bmob.v3.listener.SaveListener; 18 19 public class MainActivity extends Activity implements View.OnClickListener { 20 private android.support.design.widget.TextInputLayout editName,editPsw; 21 //此處定義自己app的ID 22 private String ID=""; 23 @Override 24 protected void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 27 setContentView(R.layout.activity_main); 28 Bmob.initialize(MainActivity.this,ID); 29 editName=(TextInputLayout)findViewById(R.id.edit_user); 30 editPsw=(TextInputLayout)findViewById(R.id.edit_psw); 31 findViewById(R.id.btn_login).setOnClickListener(this); 32 findViewById(R.id.btn_register).setOnClickListener(this); 33 } 34 35 @Override 36 public void onClick(View v) { 37 switch (v.getId()){ 38 case R.id.btn_login: 39 editName.setError(""); 40 editPsw.setError(""); 41 Login(); 42 break; 43 case R.id.btn_register: 44 Intent intentRegister=new Intent(MainActivity.this,RegisterActivity.class); 45 startActivity(intentRegister); 46 break; 47 } 48 } 49 private void Login(){ 50 String name=editName.getEditText().getText().toString(); 51 String psw=editPsw.getEditText().getText().toString(); 52 if (name.equals("")){ 53 editName.setError("用戶名不能為空"); 54 editName.requestFocus(); 55 return ; 56 }else if (psw.equals("")){ 57 editPsw.setError("密碼不能為空"); 58 editPsw.requestFocus(); 59 return ; 60 } 61 //獲取一個BmobUser對象,調用login函數,如果登錄成功,跳轉到列表界面,否則提示錯誤 62 BmobUser user=new BmobUser(); 63 user.setUsername(name); 64 user.setPassword(psw); 65 user.login(new SaveListener<BmobUser>() { 66 @Override 67 public void done(BmobUser bmobUser, BmobException e) { 68 if (e==null){ 69 Toast.makeText(MainActivity.this,"登錄成功",Toast.LENGTH_SHORT).show(); 70 Intent i=new Intent(MainActivity.this,ListActivity.class); 71 startActivity(i); 72 }else{ 73 Toast.makeText(MainActivity.this,"用戶名或密碼錯誤",Toast.LENGTH_SHORT).show(); 74 } 75 } 76 }); 77 } 78 }View Code
(2)註冊界面:RegisterActivity——主要實現用戶名註冊,獲取到用戶輸入的數據,並上傳到雲端
1 package base.secret; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.design.widget.TextInputLayout; 7 import android.view.View; 8 import android.view.Window; 9 import android.widget.Toast; 10 11 import cn.bmob.v3.Bmob; 12 import cn.bmob.v3.BmobUser; 13 import cn.bmob.v3.exception.BmobException; 14 import cn.bmob.v3.listener.SaveListener; 15 16 17 public class RegisterActivity extends Activity implements View.OnClickListener { 18 private String ID=""; 19 private android.support.design.widget.TextInputLayout editName,editPsw,editAgainPsw; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 24 setContentView(R.layout.activity_register); 25 Bmob.initialize(RegisterActivity.this,ID); 26 editName=(TextInputLayout)findViewById(R.id.register_user); 27 editPsw=(TextInputLayout)findViewById(R.id.register_psw); 28 editAgainPsw=(TextInputLayout)findViewById(R.id.register_again_psw); 29 findViewById(R.id.btn_register2).setOnClickListener(this); 30 } 31 32 @Override 33 public void onClick(View v) { 34 String name=editName.getEditText().getText().toString(); 35 String psw=editPsw.getEditText().getText().toString(); 36 String psw_again=editAgainPsw.getEditText().getText().toString(); 37 editName.setError(""); 38 editPsw.setError(""); 39 editAgainPsw.setError(""); 40 if (isValid(name,psw,psw_again)) storeUserMessage(name,psw); 41 } 42 public void storeUserMessage(String name,String psw){ 43 //保存用戶信息到雲端 44 BmobUser user=new BmobUser(); 45 user.setUsername(name); 46 user.setPassword(psw); 47 user.signUp(new SaveListener<BmobUser>() { 48 @Override 49 public void done(BmobUser bmobUser, BmobException e) { 50