安卓简单登录与注册
来源:互联网 发布:建筑信息模型软件培训 编辑:程序博客网 时间:2024/04/29 23:41
首先是效果图
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 ,先保存一个标识,然后通过改变标识.每次进去先获取标识.
阅读全文
0 0
- 安卓简单登录与注册
- 安卓通过Json注册登录
- 安卓注册登录界面示例
- 安卓登陆与注册
- 实现简单登录注册
- 简单注册登录请求
- 安卓开发中的登录注册及base64加密
- 基于tigase服务器的安卓客户端开发---注册登录
- 简单安卓QQ登录界面
- 一个简单的PHP登录与注册程序源码
- 关于APIcloud中的登录与注册的简单实现
- Ajax实现简单的登录验证与帐号注册
- struts2 注册与登录
- 登录与注册
- 模拟注册与登录
- 模拟注册与登录
- JDBC:登录与注册
- 安卓第三方友盟登录与分享
- java中对时间处理(三)----取当前时间的向前30天
- centOS6.9 防火墙的关闭以及开启
- tar命令的详细解释
- C# IDisposable接口与using关键字
- touchGFX 初探
- 安卓简单登录与注册
- 全面解析JS中的this机制
- jdbc中想同时使用两个sql查询语句,必须创建两套statement和resultSet
- Ubuntu 10.10安装SSH
- 通信加密的一些理解
- 练习 18:日志:/var/log,rsyslog,logger
- unity & php上传文件
- Deep Learning Exercise 1-3
- 神奇的Timer