您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > Android手机通用登陆界面代码设计
Android手机通用登陆界面代码设计在众多应用中,登陆界面是很多软件不可缺少的部分。本登陆界面使用SQLite设计数据库,便于与其他需求接口,下面分文件列出全部代码,欢迎指正。//DataBaseHelper.java数据库文件importandroid.content.Context;importandroid.database.sqlite.SQLiteDatabase;importandroid.database.sqlite.SQLiteDatabase.CursorFactory;importandroid.database.sqlite.SQLiteOpenHelper;importandroid.util.Log;publicclassDataBaseHelperextendsSQLiteOpenHelper{publicDataBaseHelper(Contextcontext,Stringname,CursorFactoryfactory,intversion){super(context,name,factory,version);}@OverridepublicvoidonCreate(SQLiteDatabase_db){_db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);}@OverridepublicvoidonUpgrade(SQLiteDatabase_db,int_oldVersion,int_newVersion){Log.w(TaskDBAdapter,Upgradingfromversion+_oldVersion+to+_newVersion+,whichwilldestroyallolddata);_db.execSQL(DROPTABLEIFEXISTS+TEMPLATE);onCreate(_db);}}//HomeActivity.java主界面程序文件importandroid.app.Activity;importandroid.app.Dialog;importandroid.content.Intent;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Toast;publicclassHomeActivityextendsActivity{ButtonbtnSignIn,btnSignUp;LoginDataBaseAdapterloginDataBaseAdapter;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);//createainstanceofSQLiteDatabaseloginDataBaseAdapter=newLoginDataBaseAdapter(this);loginDataBaseAdapter=loginDataBaseAdapter.open();//GetTheRefferenceOfButtonsbtnSignIn=(Button)findViewById(R.id.buttonSignIN);btnSignUp=(Button)findViewById(R.id.buttonSignUP);//SetOnClickListeneronSignUpbuttonbtnSignUp.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){//TODOAuto-generatedmethodstub///CreateIntentforSignUpActivityandStartTheActivityIntentintentSignUP=newIntent(getApplicationContext(),SignUPActivity.class);startActivity(intentSignUP);}});}//MethostohandleClickEventofSignInButtonpublicvoidsignIn(ViewV){finalDialogdialog=newDialog(HomeActivity.this);dialog.setContentView(R.layout.login);dialog.setTitle(Login);//gettheRefferencesofviewsfinalEditTexteditTextUserName=(EditText)dialog.findViewById(R.id.editTextUserNameToLogin);finalEditTexteditTextPassword=(EditText)dialog.findViewById(R.id.editTextPasswordToLogin);ButtonbtnSignIn=(Button)dialog.findViewById(R.id.buttonSignIn);btnSignIn.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){StringuserName=editTextUserName.getText().toString();Stringpassword=editTextPassword.getText().toString();//fetchthePasswordformdatabaseforrespectiveusernameStringstoredPassword=loginDataBaseAdapter.getSinlgeEntry(userName);//checkiftheStoredpasswordmatcheswithPasswordenteredbyuserif(password.equals(storedPassword)){Toast.makeText(HomeActivity.this,Congrats:LoginSuccessfull,Toast.LENGTH_LONG).show();dialog.dismiss();}else{Toast.makeText(HomeActivity.this,UserNameorPassworddoesnotmatch,Toast.LENGTH_LONG).show();}}});dialog.show();}@OverrideprotectedvoidonDestroy(){super.onDestroy();loginDataBaseAdapter.close();}}//LoginDataBaseAdapter.javaimportandroid.content.ContentValues;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.SQLException;importandroid.database.sqlite.SQLiteDatabase;publicclassLoginDataBaseAdapter{staticfinalStringDATABASE_NAME=login.db;staticfinalintDATABASE_VERSION=1;publicstaticfinalintNAME_COLUMN=1;//TODO:Createpublicfieldforeachcolumninyourtable.//SQLStatementtocreateanewdatabase.staticfinalStringDATABASE_CREATE=createtable+LOGIN+(+ID+integerprimarykeyautoincrement,+USERNAMEtext,PASSWORDtext);;//VariabletoholdthedatabaseinstancepublicSQLiteDatabasedb;//Contextoftheapplicationusingthedatabase.privatefinalContextcontext;//Databaseopen/upgradehelperprivateDataBaseHelperdbHelper;publicLoginDataBaseAdapter(Context_context){context=_context;dbHelper=newDataBaseHelper(context,DATABASE_NAME,null,DATABASE_VERSION);}publicLoginDataBaseAdapteropen()throwsSQLException{db=dbHelper.getWritableDatabase();returnthis;}publicvoidclose(){db.close();}publicSQLiteDatabasegetDatabaseInstance(){returndb;}publicvoidinsertEntry(StringuserName,Stringpassword){ContentValuesnewValues=newContentValues();//Assignvaluesforeachrow.newValues.put(USERNAME,userName);newValues.put(PASSWORD,password);//Inserttherowintoyourtabledb.insert(LOGIN,null,newValues);///Toast.makeText(context,ReminderIsSuccessfullySaved,Toast.LENGTH_LONG).show();}publicintdeleteEntry(StringUserName){//Stringid=String.valueOf(ID);Stringwhere=USERNAME=?;intnumberOFEntriesDeleted=db.delete(LOGIN,where,newString[]{UserName});//Toast.makeText(context,NumberfoEntryDeletedSuccessfully:+numberOFEntriesDeleted,Toast.LENGTH_LONG).show();returnnumberOFEntriesDeleted;}publicStringgetSinlgeEntry(StringuserName){Cursorcursor=db.query(LOGIN,null,USERNAME=?,newString[]{userName},null,null,null);if(cursor.getCount()1)//UserNameNotExist{cursor.close();returnNOTEXIST;}cursor.moveToFirst();Stringpassword=cursor.getString(cursor.getColumnIndex(PASSWORD));cursor.close();returnpassword;}publicvoidupdateEntry(StringuserNa
本文标题:Android手机通用登陆界面代码设计
链接地址:https://www.777doc.com/doc-4853490 .html