布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="@+id/layout_login"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/login_name"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="220dp"
android:hint=" 請輸入用戶名"/>
<EditText
android:id="@+id/login_pwd"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="280dp"
android:hint=" 請輸入密碼"/>
<Button
android:id="@+id/login"
android:layout_centerHorizontal="true"
android:layout_width="250dp"
android:layout_height="50dp"
android:background="@drawable/layout_button"
android:layout_marginTop="340dp"
android:text="登陸"
android:textColor="#fff"
android:textSize="20sp"/>
<Button
android:id="@+id/login_regist"
android:layout_centerHorizontal="true"
android:layout_width="250dp"
android:layout_height="50dp"
android:background="@drawable/layout_button"
android:layout_marginTop="400dp"
android:text="注冊"
android:textColor="#fff"
android:textSize="20sp"/>
</RelativeLayout>
<RelativeLayout
android:visibility="gone"
android:id="@+id/layout_register"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/register_name"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="220dp"
android:hint=" 請輸入用戶名"/>
<EditText
android:id="@+id/code"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="55dp"
android:layout_marginTop="280dp"
android:hint=" 請輸入驗證碼"/>
<Button
android:id="@+id/huoqu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/code"
android:layout_below="@id/register_name"
android:layout_marginTop="10dp"
android:text="獲取驗證碼"
android:background="#03A9F4"
android:textColor="#fff"
/>
<EditText
android:id="@+id/register_pwd"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="330dp"
android:hint=" 請輸入密碼"/>
<Button
android:id="@+id/register"
android:layout_centerHorizontal="true"
android:layout_width="250dp"
android:layout_height="50dp"
android:background="@drawable/layout_button"
android:layout_marginTop="400dp"
android:text="注冊"
android:textColor="#fff"
android:textSize="20sp"/>
</RelativeLayout>
</RelativeLayout>
创建一个广播接收者
package com.example.day1016homework;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "MyReceiver";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("com.ludan.egg")){
Log.i(TAG, "onReceive: 接到通知了");
Bundle b1 = intent.getBundleExtra("b1");
String code = b1.getString("code");
Notification.Builder builder = new Notification.Builder(context);
builder.setSmallIcon(R.drawable.a);
builder.setContentTitle("验证码");
builder.setContentText("您的验证码是:"+code);
Notification build = builder.build();
NotificationManager systemService = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
builder.setPriority(Notification.PRIORITY_MAX);
builder.setDefaults(Notification.DEFAULT_ALL);
systemService.notify(1,build);
}
}
}
清单列表加广播用的意图
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.day1016homework">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.ludan.egg"></action>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
创建一个类继承SQLiteOpenHelper
package com.example.day1016homework.sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class MySql extends SQLiteOpenHelper {
private List<String> s = new ArrayList<>();
public MySql( Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
s.add("A8sy");
s.add("aEt8");
s.add("6U54");
s.add("ExY3");
s.add("ISp4");
s.add("dWV4");
s.add("WM71");
s.add("A8sy");
s.add("asTC");
s.add("YFex");
db.execSQL("create table user(name varchar(30),password varchar(30))");
db.execSQL("create table code(id integer primary key autoincrement , icode varchar(30))");
for (int i = 0; i < s.size(); i++) {
String s = this.s.get(i).toString();
String sql = "insert into code values(?,?)";
db.execSQL(sql,new Object[]{null,s});
}
db.beginTransaction();
db.setTransactionSuccessful();
db.endTransaction();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Main
package com.example.day1016homework;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.example.day1016homework.sql.MySql;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private RelativeLayout layoutLogin;
private EditText loginName;
private EditText loginPwd;
private Button login;
private Button loginRegist;
private RelativeLayout layoutRegister;
private EditText registerName;
private EditText code;
private Button huoqu;
private EditText registerPwd;
private Button register;
private MySql mySql;
private SQLiteDatabase db;
private int index = 1;
private String yanzhengma;
private String chaname;
private String chapassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intend();
mySql = new MySql(this, "together.db", null, 1);
db = mySql.getReadableDatabase();
regisert_new();
login_new();
loginRegist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layoutLogin.setVisibility(View.GONE);
layoutRegister.setVisibility(View.VISIBLE);
}
});
}
private void login_new() {
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = loginName.getText().toString();
String pwd = loginPwd.getText().toString();
if (name.equals("")||pwd.equals("")){
Toast.makeText(MainActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
}
Cursor cursor = db.rawQuery("select * from user where name = ? and password = ?", new String[]{name,pwd});
if (cursor!=null){
while (cursor.moveToNext()){
String name1 = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
if (name1.equals(name)&&password.equals(pwd)){
Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
}
cursor.close();
}
});
}
private void regisert_new() {
final String name = registerName.getText().toString();
String pwd = registerPwd.getText().toString();
huoqu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = registerName.getText().toString();
if (s.equals("")){
Toast.makeText(MainActivity.this, "手机号(用户名)不能空", Toast.LENGTH_SHORT).show();
return;
}else {
Cursor cursor = db.rawQuery("select * from code where id = ?", new String[]{String.valueOf(index)});
index++;
if (index == 10){
index =1;
}
if (cursor != null){
while(cursor.moveToNext()){
yanzhengma = cursor.getString(cursor.getColumnIndex("icode"));
}
}
cursor.close();
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("code",yanzhengma);
intent.putExtra("b1",bundle);
intent.setAction("com.ludan.egg");
sendBroadcast(intent);
}
}
});
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Editable text = code.getText();
if (!text.toString().equals(yanzhengma)){
Toast.makeText(MainActivity.this, "验证码不正确", Toast.LENGTH_SHORT).show();
return;
}else{
String s = registerPwd.getText().toString();
String s1 = registerName.getText().toString();
Pattern compile = Pattern.compile("[\\w]{6,16}");
Matcher matcher = compile.matcher(s);
if (matcher.matches()){
String sql = "insert into user values (?,?)";
db.execSQL(sql,new Object[]{s1,s});
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
layoutLogin.setVisibility(View.VISIBLE);
layoutRegister.setVisibility(View.GONE);
}else{
Toast.makeText(MainActivity.this, "密码格式不正确", Toast.LENGTH_SHORT).show();
}
}
}
});
}
private void intend() {
layoutLogin = (RelativeLayout) findViewById(R.id.layout_login);
loginName = (EditText) findViewById(R.id.login_name);
loginPwd = (EditText) findViewById(R.id.login_pwd);
login = (Button) findViewById(R.id.login);
loginRegist = (Button) findViewById(R.id.login_regist);
layoutRegister = (RelativeLayout) findViewById(R.id.layout_register);
registerName = (EditText) findViewById(R.id.register_name);
code = (EditText) findViewById(R.id.code);
huoqu = (Button) findViewById(R.id.huoqu);
registerPwd = (EditText) findViewById(R.id.register_pwd);
register = (Button) findViewById(R.id.register);
}
}