实现简单登录注册

登录页面布局  main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:gravity="center_horizontal"
        android:textColor="#8a2be2"
        android:textSize="35dp"
        android:text="登录界面" />
    <TextView
        android:id="@+id/txtname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/login"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="30dp"
        android:textSize="28dp"
        android:text="用户帐号:"/>
    <EditText
        android:id="@+id/edname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_below="@id/login"
        android:layout_toRightOf="@id/txtname"
        android:layout_alignParentRight="true"
        android:hint="请输入用户帐号"/>
    <TextView
        android:id="@+id/txtpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtname"
        android:layout_marginRight="5dp"
        android:textSize="28dp"
        android:text="用户密码:"/>
    <EditText
        android:id="@+id/edpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/edname"
        android:layout_toRightOf="@id/txtpassword"
        android:layout_alignParentRight="true"
        android:inputType="textPassword"
        android:hint="请输入用户密码"/>
    <LinearLayout
        android:layout_below="@id/edpassword"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal" >
        <Button
            android:id="@+id/btregister"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginRight="20dp"
            android:text="用户注册"/>
        <Button
            android:id="@+id/btlogin"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="用户登录"/>
    </LinearLayout>

</RelativeLayout>
注册页面布局 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >


    <TextView
        android:id="@+id/txt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:gravity="center_horizontal"
        android:text="注册界面"
        android:textColor="#8a2be2"
        android:textSize="35dp" />

    <TextView
        android:id="@+id/txtname1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txt1"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="5dp"
        android:text="帐号:"
        android:textSize="28dp" />

    <EditText
        android:id="@+id/edname1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/txt1"
        android:layout_toRightOf="@id/txtname1"
        android:layout_marginBottom="30dp" />

    <TextView
        android:id="@+id/txtpassword1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/txtname1"
        android:layout_marginRight="5dp"
        android:text="密码:"
        android:textSize="28dp" />

    <EditText
        android:id="@+id/edpassword1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/edname1"
        android:layout_toRightOf="@id/txtpassword1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/edpassword1"
        android:layout_marginTop="30dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btregister1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="确定注册" />
    </LinearLayout>
</RelativeLayout>
登录页面代码
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    // 帐号和密码
    private EditText edname;
    private EditText edpassword;

    private Button btregister;
    private Button btlogin;
    // 创建SQLite数据库
    public static SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edname = (EditText) findViewById(R.id.edname);
        edpassword = (EditText) findViewById(R.id.edpassword);
        btregister = (Button) findViewById(R.id.btregister);
        btlogin = (Button) findViewById(R.id.btlogin);
        db = SQLiteDatabase.openOrCreateDatabase(MainActivity.this.getFilesDir().toString()
                + "/test.dbs", null);
        // 跳转到注册界面
        btregister.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, RegistersActivity.class);
                startActivity(intent);
            }
        });
        btlogin.setOnClickListener(new LoginListener());
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        db.close();
    }


    class LoginListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String name = edname.getText().toString();
            String password = edpassword.getText().toString();
            if (name.equals("") || password.equals("")) {
                // 弹出消息框
                new AlertDialog.Builder(MainActivity.this).setTitle("错误")
                        .setMessage("帐号或密码不能空").setPositiveButton("确定", null)
                        .show();
            } else {
                isUserinfo(name, password);
            }
        }

        // 判断输入的用户是否正确
        public Boolean isUserinfo(String name, String pwd) {
            try{
                String str="select * from tb_user where name=? and password=?";
                Cursor cursor = db.rawQuery(str, new String []{name,pwd});
                if(cursor.getCount()<=0){
                    new AlertDialog.Builder(MainActivity.this).setTitle("错误")
                            .setMessage("帐号或密码错误!").setPositiveButton("确定", null)
                            .show();
                    return false;
                }else{
                    new AlertDialog.Builder(MainActivity.this).setTitle("正确")
                            .setMessage("成功登录").setPositiveButton("确定", null)
                            .show();
                    return true;
                }

            }catch(SQLiteException e){
                createDb();
            }
            return false;
        }

    }
    // 创建数据库和用户表
    public void createDb() {
        db.execSQL("create table tb_user( name varchar(30) primary key,password varchar(30))");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
      //  getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
注册页面代码
e;  
        } catch (Exception e) {  
            main.createDb();  
        }  
        return false;  
    }  
  
} 


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值