简单java登录页面android_安卓实现简单的登录注册

本文展示了如何在Android应用中创建一个简单的登录页面。核心代码包括获取用户输入的账号和密码,进行数据库查询验证登录,并提供了注册功能。当点击登录按钮时,如果输入为空则显示提示,否则检查用户名和密码是否匹配。匹配成功则跳转到登录后的界面,不匹配则显示错误信息。
摘要由CSDN通过智能技术生成

【实例简介】

【实例截图】

8d16fa33e40239d7719294f427015d29.png

99279aeec138f7ff794292b97421160e.png

【核心代码】

package com.example.test1;

import android.app.Activity;

import android.app.AlertDialog;

import android.app.AlertDialog.Builder;

import android.content.DialogInterface;

import android.content.Intent;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.view.KeyEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.LinearLayout;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

/** Called when the activity is first created. */

//private static final String PREFS_NAME = "MyInfo";//用来保存用户信息

public static EditText Num,Pwd;

private static Button Login,Post,pwdback;

//public static CheckBox RememberPwd,AutoLogin;

private DBHelper dbHelper;

public static SQLiteDatabase sdb;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// AppConnect.getInstance(this);

//LinearLayout container =(LinearLayout)findViewById(R.id.AdLinearLayout);

//new AdView(this,container).DisplayAd();

//获得实例对象

Num=(EditText) findViewById(R.id.et_Num);

Pwd=(EditText) findViewById(R.id.et_Pwd);

Login=(Button) findViewById(R.id.btn_Login);

Post=(Button) findViewById(R.id.Post);

pwdback = (Button) findViewById(R.id.pwdback);

Post.setOnClickListener(this);

Login.setOnClickListener(this);

pwdback.setOnClickListener(this);

dbHelper=new DBHelper(this,"Info.db");

//LoadUserDate();

}

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

switch(v.getId()){

case R.id.btn_Login:

if(Num.getText().toString().equals("")){

Toast.makeText(MainActivity.this,"姓名不能为空 ", Toast.LENGTH_SHORT).show();

return;

}

if(Pwd.getText().toString().equals("")){

Toast.makeText(MainActivity.this,"号码不能为空 ", Toast.LENGTH_SHORT).show();

return;

}

//SaveUserDate();

boolean bool=login(Num.getText().toString(),Pwd.getText().toString());

if(bool){

Toast.makeText(MainActivity.this, "登录成功!", Toast.LENGTH_LONG).show();

Intent intent = new Intent();

intent.putExtra("name",Num.getText().toString());

intent.setClass(MainActivity.this, AfterLogin.class);

MainActivity.this.startActivity(intent);

}else{

Toast.makeText(MainActivity.this, "用户名或密码有误!", Toast.LENGTH_LONG).show();

return;

}

break;

case R.id.Post:

Intent intent=new Intent(MainActivity.this,Register.class);

startActivity(intent);

break;

case R.id.pwdback:

//System.out.println("被响应");

Intent intent1 = new Intent();

//intent.putExtra("name",Num.getText().toString());

intent1.setClass(MainActivity.this, pwdback.class);

MainActivity.this.startActivity(intent1);

break;

}

}

//登录

public boolean login(String username,String password){

String name = new Security().Encryption(username);

String pwd = new Security().Encryption(password);

/*SQLiteDatabase*/ sdb=dbHelper.getReadableDatabase();

String sql="select * from people where name=? and pwd=?";

Cursor cursor=sdb.rawQuery(sql, new String[]{name,pwd});

if(cursor.moveToFirst()==true){

cursor.close();

return true;

}

return false;

}

//退出

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_BACK ) {

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

builder.setIcon(android.R.drawable.ic_dialog_info);

builder.setMessage("确定要退出?");

builder.setTitle("提示");

builder.setPositiveButton("确认",

new android.content.DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

MainActivity.this.finish();

}

});

builder.setNegativeButton("取消",

new android.content.DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

});

builder.create().show();

}

return false;

}

// /**

// * 保存用户信息

// */

//private void SaveUserDate() {

载入配置文件

//SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);

写入配置文件

//Editor spEd = sp.edit();

//if (RememberPwd.isChecked()) {

//spEd.putBoolean("isSave", true);

//spEd.putString("name", Num.getText().toString());

//spEd.putString("password", Pwd.getText().toString());

//} else {

//spEd.putBoolean("isSave", false);

//spEd.putString("name", "");

//spEd.putString("password", "");

//}

//spEd.commit();

//}

//

///**

// * 载入已记住的用户信息

// */

//private void LoadUserDate() {

//SharedPreferences sp = getSharedPreferences(PREFS_NAME, 0);

//

//if (sp.getBoolean("isSave", false)) {

//String username = sp.getString("name", "");

//String userpassword = sp.getString("password", "");

//if (!("".equals(username) && "".equals(userpassword))) {

//Num.setText(username);

//Pwd.setText(userpassword);

//RememberPwd.setChecked(true);

//}

//}

//}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值