点击获取验证码 通知 广播 弹出 登录注册

布局

<?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;//用来取验证码的id
    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();
//                Toast.makeText(MainActivity.this, name+"--"+pwd, Toast.LENGTH_SHORT).show();
                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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值