1、我昨天的成就:完成了登录功能的连接数据库功能
2、遇到什么困难:在判断查询到数据库中有无已注册的数据遇到困难
3、今天的任务:完善登录注册功能
package com.example.lenovo.share; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends AppCompatActivity { private Button login; private Button register; //申明控件 private EditText edit_username; private EditText edit_password; //申明参数 String username; String password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); //调用init方法 init(); } private void init() { //获取相应控件 edit_username=findViewById(R.id.login_user); edit_password=findViewById(R.id.login_password); login=findViewById(R.id.login_login); register=findViewById(R.id.login_register); //创建login点击事件 login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //获取相应值 username=edit_username.getText().toString().trim(); password=edit_password.getText().toString().trim(); Toast.makeText(LoginActivity.this, "账号:"+username+"密码:"+password, Toast.LENGTH_SHORT).show(); //判断输入内容 if(TextUtils.isEmpty(username)){ Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show(); return; }else if(TextUtils.isEmpty(password)){ Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show(); return; }else if(notExitUserAndPsd(username,password)){ Toast.makeText(getApplicationContext(),"账号或密码错误!",Toast.LENGTH_SHORT).show(); return; }else{ Intent intent=new Intent(LoginActivity.this,MainActivity.class); Toast.makeText(getApplicationContext(),"登陆成功!",Toast.LENGTH_SHORT).show(); startActivity(intent); LoginActivity.this.finish(); } } }); //创建注册点击事件 register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(LoginActivity.this,RegisterActivity.class); startActivity(intent); } }); } //方法:查询数据库中的账号,密码 private boolean notExitUserAndPsd(String username, String password) { // Toast.makeText(getApplicationContext(),"在query!",Toast.LENGTH_SHORT).show(); DbHelper helper = new DbHelper(this,"share.db",null,1); SQLiteDatabase db = helper.getWritableDatabase(); //查询数据库所有的数据 Cursor c = db.query("user",null,"username=? and password=?",new String[]{username,password},null,null,null); //Toast.makeText(getApplicationContext(),c.getString(c.getColumnIndex("username")),Toast.LENGTH_SHORT).show(); //boolean类型的b用于判断是否查询到,true表示没找到,false表示找到了 boolean b = true; if(c!=null&&c.getCount()>=1) { Toast.makeText(getApplicationContext(),"姓名",Toast.LENGTH_SHORT).show(); // Toast.makeText(getApplicationContext(),"姓名"+c.getString(c.getColumnIndex("username")),Toast.LENGTH_SHORT).show(); b=false; this.finish(); } return b; } }