登陆注册技能练习

xml布局(登陆界面)

<ImageView
        android:layout_weight="3"
        android:src="@mipmap/a"
        android:scaleType="fitXY"
        android:layout_width="match_parent"
        android:layout_height="0dp" />
    <LinearLayout
        android:layout_weight="3"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="20dp">
        <EditText
            android:id="@+id/editText_name"
            android:layout_weight="1"
            android:drawableLeft="@mipmap/b"
            android:hint="3132744921@qq.com"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <EditText
            android:id="@+id/editText_pwd"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:drawableLeft="@mipmap/b"
            android:hint="密码"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/login"
            android:layout_weight="1"
            android:text="登陆"
            android:textSize="30dp"
            android:textColor="#fff"
            android:background="#EC859A"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <LinearLayout
        android:layout_weight="3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="20dp">
        <Button
            android:id="@+id/button_update"
            android:layout_weight="1"
            android:text="忘记密码"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:background="@null"/>
        <Button
            android:layout_width="1dp"
            android:layout_height="wrap_content"
            android:background="#000"
            android:layout_marginTop="50dp"/>
        <Button
            android:id="@+id/button_register"
            android:layout_weight="1"
            android:text="立即注册"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:background="@null"/>
    </LinearLayout>

xml布局(注册)

<RelativeLayout
        android:background="#EC859A"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_out"
            android:layout_width="50dp"
            android:layout_height="80dp"
            android:text="<"
            android:textColor="#fff"
            android:background="@null"
            android:textSize="30dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="80dp"
            android:text="注册"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:textSize="25dp"
            android:textColor="#fff"
            />
    </RelativeLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp">
        <EditText
            android:id="@+id/editText_name"
            android:layout_weight="1"
            android:drawableLeft="@mipmap/b"
            android:hint="手机号"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <RelativeLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <EditText
                android:id="@+id/editText_yzm"
                android:drawableLeft="@mipmap/b"
                android:hint="验证码"
                android:layout_toLeftOf="@id/button_sendYzm"
                android:layout_alignParentLeft="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <Button
                android:id="@+id/button_sendYzm"
                android:layout_alignParentRight="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="发送验证码"

                />
        </RelativeLayout>


        <EditText
            android:id="@+id/editText_pwd"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:drawableLeft="@mipmap/b"
            android:hint="密码"
            android:layout_height="match_parent" />
        <Button
            android:id="@+id/register"
            android:layout_weight="1"
            android:text="立即注册"
            android:textSize="30dp"
            android:layout_marginTop="50dp"
            android:textColor="#fff"
            android:background="#EC859A"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

mySqliteHelp

public class MySqlHelp extends SQLiteOpenHelper {
    public MySqlHelp(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table yzm(num varchar(30))");
        sqLiteDatabase.execSQL("create table user(phone varchar(30),pwd varchar(30))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

MainActivity1

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText_name;
    private EditText editText_pwd;
    private Button login;
    private Button button_update;
    private Button button_register;
    private MySqlHelp mySqlHelp;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        editText_name = (EditText) findViewById(R.id.editText_name);
        editText_pwd = (EditText) findViewById(R.id.editText_pwd);
        login = (Button) findViewById(R.id.login);
        button_update = (Button) findViewById(R.id.button_update);
        button_register = (Button) findViewById(R.id.button_register);

        login.setOnClickListener(this);
        button_update.setOnClickListener(this);
        button_register.setOnClickListener(this);

        //验证码数据库添加10条数据
        addSqlite();

    }
    //验证码数据库添加10条数据
    private void addSqlite() {
        mySqlHelp = new MySqlHelp(this,"yzm.db",null,1);
        db = mySqlHelp.getReadableDatabase();

        for (int i = 0; i < 10; i++) {
            db.execSQL("insert into yzm values(?)",new Object[]{i+""+i+""+i});
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                //判断非空,查询有无下一条数据,获取数据,按条件查找
                    Cursor cursor1 = db.rawQuery("select * from user", null);
                    if (cursor1!=null){
                        Log.e("tag", "onClick: +++"+cursor1);
                        while (cursor1.moveToNext()){
                            Log.e("tag", "onClick: ---");
                            String phone = cursor1.getString(cursor1.getColumnIndex("phone"));
                            String pwd = cursor1.getString(cursor1.getColumnIndex("pwd"));
                            if (editText_name.getText().toString().equals(phone)&&editText_pwd.getText().toString().equals(pwd)){
                                Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
                            }else {
                                Toast.makeText(this, "登陆失败", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }
                    cursor1.close();
                break;
            case R.id.button_update:

                break;
            case R.id.button_register:
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                break;
        }
    }

}

MainActivity2

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {

    private EditText editText_name;
    private EditText editText_yzm;
    private EditText editText_pwd;
    private Button button_out;
    private Button button_sendYzm;
    private Button register;
    private List<String> list = new ArrayList<>();
    private int index = 0;
    private String number = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
    }

    private void initView() {
        button_out = (Button) findViewById(R.id.button_out);
        editText_name = (EditText) findViewById(R.id.editText_name);
        editText_yzm = (EditText) findViewById(R.id.editText_yzm);
        button_sendYzm = (Button) findViewById(R.id.button_sendYzm);
        editText_pwd = (EditText) findViewById(R.id.editText_pwd);
        register = (Button) findViewById(R.id.register);

        button_out.setOnClickListener(this);
        button_sendYzm.setOnClickListener(this);
        register.setOnClickListener(this);


        //把数据库得验证码加入到集合中
        MySqlHelp yzm = new MySqlHelp(this, "yzm.db", null, 1);
        SQLiteDatabase db = yzm.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from yzm", null);
        if (cursor!=null){
            while (cursor.moveToNext()){
                String yzm1 = cursor.getString(cursor.getColumnIndex("num"));
                list.add(yzm1);
            }
        }

    }
    //注册时输入手机号后,才能点击发送验证码
    private void selectYzm() {
        if (!editText_name.getText().toString().equals("")){
            //发送广播
            Intent intent = new Intent();
            intent.setAction("com.lu.broad");
            Bundle bundle = new Bundle();
            bundle.putString("number",list.get(index));
            intent.putExtras(bundle);
            sendBroadcast(intent);
            //弹出通知
            Notification.Builder builder = new Notification.Builder(this);
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setContentTitle("消息通知");
            builder.setContentText(list.get(index));
            NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            Notification build = builder.build();
            manager.notify(1,build);
            number = list.get(index);
            index++;
            if (index == 9){
                index =0;
            }
            Toast.makeText(this, "已发送验证码", Toast.LENGTH_SHORT).show();
            button_sendYzm.setClickable(true);
        }else {
//            button_sendYzm.setClickable(false);
            Toast.makeText(this, "不可点击,请填写手机号", Toast.LENGTH_SHORT).show();
        }
    }
    //判断验证码,密码是否输入正确
    private void registerIf() {
        if (editText_yzm.getText().toString().equals(number)){
            String trim = editText_pwd.getText().toString().trim();
            Pattern pattern = Pattern.compile("[0-9a-zA-Z]{6,11}");
            Matcher matcher = pattern.matcher(trim);
            boolean matches = matcher.matches();

            if (!matches){
                Toast.makeText(this, "密码输入有误", Toast.LENGTH_SHORT).show();
                return;
            }else{
                MySqlHelp mySqlHelp = new MySqlHelp(this,"yzm.db",null,1);
                SQLiteDatabase readableDatabase = mySqlHelp.getReadableDatabase();
                String phone1 = editText_name.getText().toString();
                String pwd1 = editText_pwd.getText().toString();
                readableDatabase.execSQL("insert into user(phone,pwd) values(?,?)",new String[]{phone1,pwd1});
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                finish();
            }
        }else {
            Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();
            return;
        }
    }
    //控件按钮点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_out:
                this.finish();
                break;
            case R.id.button_sendYzm:
                selectYzm();
                break;
            case R.id.register:
                registerIf();
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值