安卓简单登录与注册

首先是效果图






1.MainXML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bawei.day1010.MainActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/etName"
            android:drawableLeft="@mipmap/ic_launcher"
            android:hint="请输入用户名"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/butClearName"
            android:layout_centerVertical="true"
            android:background="@mipmap/ic_launcher"
            android:layout_alignParentRight="true"
            android:layout_width="30dp"
            android:layout_height="30dp"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/etPwd"
            android:drawableLeft="@mipmap/ic_launcher"
            android:hint="密码"
            android:inputType="textPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <CheckBox
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:id="@+id/pwdShow"
            android:button="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/butClearPwd"
            android:layout_centerVertical="true"
            android:background="@mipmap/ic_launcher"
            android:layout_toLeftOf="@id/pwdShow"
            android:layout_width="30dp"
            android:layout_height="30dp"/>
    </RelativeLayout>
    <Button
        android:id="@+id/butLog"
        android:text="登录"
        android:layout_gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:background="@drawable/nullstroke"
            android:textColor="#f00"
            android:id="@+id/butReg"
            android:text="注册"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:background="@drawable/nullstroke"
            android:textColor="#f00"
            android:id="@+id/butForgetPwd"
            android:text="忘记密码"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>
</LinearLayout>
2.空Shape


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>


3.Helper

public class MyHelper extends SQLiteOpenHelper {
    public MyHelper(Context context) {
        super(context, "mydb", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table mytb(id integer primary key autoincrement,name text,pwd text)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists mydb");
        onCreate(db);
    }
}


4.MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TABLE = "mytb";
    /**
     * 请输入用户名
     */
    private EditText mEtName;
    /**
     * 密码
     */
    private EditText mEtPwd;
    private CheckBox mPwdShow;
    private Button mButClearName;
    private Button mButClearPwd;
    /**
     * 登录
     */
    private Button mButLog;
    /**
     * 注册
     */
    private Button mButReg;
    /**
     * 忘记密码
     */
    private Button mButForgetPwd;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        db = new MyHelper(this).getWritableDatabase();

    }

    private void initView() {
        mEtName = (EditText) findViewById(R.id.etName);
        mEtPwd = (EditText) findViewById(R.id.etPwd);
        mPwdShow = (CheckBox) findViewById(R.id.pwdShow);

        mPwdShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //选择状态 显示明文--设置为可见的密码
                    mEtPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    //默认状态显示密码--设置文本 要一起写才能起作用 InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD
                    mEtPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
            }
        });
        mButClearName = (Button) findViewById(R.id.butClearName);
        mButClearName.setOnClickListener(this);
        mButClearPwd = (Button) findViewById(R.id.butClearPwd);
        mButClearPwd.setOnClickListener(this);
        mButLog = (Button) findViewById(R.id.butLog);
        mButLog.setOnClickListener(this);
        mButReg = (Button) findViewById(R.id.butReg);
        mButReg.setOnClickListener(this);
        mButForgetPwd = (Button) findViewById(R.id.butForgetPwd);
        mButForgetPwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.butClearName:
                mEtName.setText("");
                break;
            case R.id.butClearPwd:
                mEtPwd.setText("");
                break;
            case R.id.butLog:
                String edNameLog1 = getEdString(mEtName);
                String edPwdLog1 = getEdString(mEtPwd);
                if(edNameLog1==null){
                    Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(edPwdLog1==null){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                String sql="select * from mytb where name=? and pwd=?";
                Cursor cursor = db.rawQuery(sql,new String[]{edNameLog1+"",edPwdLog1+""});
                if(!cursor.moveToNext()){
                        Toast.makeText(MainActivity.this,"用户名或密码不匹配",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.butReg:
                String edNameReg = getEdString(mEtName);
                String edPwdReg= getEdString(mEtPwd);
                if(edNameReg==null){
                    Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(edPwdReg==null){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                //保存到数据库
                ContentValues values=new ContentValues();
                values.put("name",edNameReg);
                values.put("pwd",edPwdReg);
                long insert = db.insert(TABLE, null, values);
                if(insert>0){
                    Toast.makeText(MainActivity.this,"注册成功,请登录",Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.butForgetPwd:

                break;
        }
    }

    /**
     * 得到editText的字符串
     */
    public String getEdString(EditText editText) {
        String s = editText.getText().toString();
        if (s != null && !s.equals("")) {
            return s;
        }
        return null;
    }

}

提示:这里可以加入自动登录的,使用SharePreferences  ,先保存一个标识,然后通过改变标识.每次进去先获取标识.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值