示例图 :

activity_main.xml :

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="Name:"/> <EditText
android:id="@+id/text_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="Input Name"
android:layout_toRightOf="@+id/t1"
android:layout_marginLeft="100dp"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/t1"
android:textSize="20dp"/>
<EditText
android:id="@+id/text_password"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="Input Password"
android:layout_below="@+id/text_name"
android:layout_toRightOf="@id/t2"
android:layout_marginLeft="65dp"
android:layout_marginRight="10dp"
/>
<Button
android:id="@+id/btn_reg"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_below="@+id/t2"
android:layout_marginTop="30dp"
android:text="注册" /> <Button
android:id="@+id/btn_del"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_below="@+id/t2"
android:layout_marginTop="30dp"
android:layout_toRightOf="@+id/btn_reg"
android:text="删除" />
<Button
android:id="@+id/btn_sel"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_below="@+id/t2"
android:layout_marginTop="30dp"
android:layout_toRightOf="@id/btn_del"
android:text="查询" />
<LinearLayout
android:id="@+id/layout"
android:layout_below="@id/btn_sel"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_width="match_parent"
>
</LinearLayout>
</RelativeLayout> MainActivity.java :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
LinearLayout Layout ;
EditText Name,Password;
Button bt_reg,bt_del,bt_sel;
List<User> userList;
private MyDatabaseHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
Layout=(LinearLayout) findViewById(R.id.layout);
Name=(EditText)findViewById(R.id.text_name);
Password=(EditText)findViewById(R.id.text_password);
bt_reg=(Button)findViewById(R.id.btn_reg);
bt_del=(Button)findViewById(R.id.btn_del);
bt_sel=(Button)findViewById(R.id.btn_sel);
bt_reg.setOnClickListener(this);
bt_del.setOnClickListener(this);
bt_sel.setOnClickListener(this);
userList=new ArrayList<User>(); }
public void onClick(View view){
switch (view.getId()) { case R.id.btn_reg:
if (Name.getText().toString().matches("[a-zA-Z]*") && Name.getText().toString().length() > 5 && Name.getText().toString().length() < 13 && Password.getText().toString().matches("[0-9]*") && Password.getText().toString().length() > 2 && Password.getText().toString().length() < 7) {
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
db.execSQL("insert into user(name,password) values (?,?)", new String[]{Name.getText().toString(), Password.getText().toString()});
db.close();
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show(); } else
Toast.makeText(MainActivity.this, "输入不合法", Toast.LENGTH_SHORT).show();
break; case R.id.btn_del:
if (Name.getText().toString().length() != 0 && Password.getText().toString().length() != 0) {
User user = null;
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * " + "from user where name=?", new String[]{Name.getText().toString()});
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
user = new User(id, name, password);
cursor.close(); if (user.getPassword().equals(Password.getText().toString())) {
db.execSQL("delete from user where name=?", new String[]{Name.getText().toString()});
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); } else
Toast.makeText(MainActivity.this, "密码不正确", Toast.LENGTH_SHORT).show(); db.close();
}
}
else if(Name.getText().toString().length() != 0 && Password.getText().toString().length() == 0){
User user = null;
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * " + "from user where name=?", new String[]{Name.getText().toString()});
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
user = new User(id, name, password);
cursor.close();
if (user.getName().equals(Name.getText().toString())) {
db.execSQL("delete from user where name=?", new String[]{Name.getText().toString()});
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show(); } else
Toast.makeText(MainActivity.this, "用户不存在", Toast.LENGTH_SHORT).show(); db.close();
} }
break; case R.id.btn_sel: Layout.removeAllViews(); if (Name.getText().toString().length() == 0 && Password.getText().toString().length() == 0) {
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from user", null); if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
User user = new User(id, name, password);
userList.add(user);
} while (cursor.moveToNext());
}
for (User user : userList) {
TextView tv = new TextView(this);
tv.setText(user.toString());
tv.setTextSize(20);
Layout.addView(tv);
}
userList.clear();
cursor.close();
db.close(); } else if (Name.getText().toString().length() != 0 && Password.getText().toString().length() == 0) { dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * " + "from user where name=?", new String[]{Name.getText().toString()});
User user = null; if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
user = new User(id, name, password);
cursor.close();
db.close(); TextView tv = new TextView(this);
tv.setText(user.toString());
tv.setTextSize(20);
Layout.addView(tv);
}
} else if (Name.getText().toString().length()!=0){
dbhelper=new MyDatabaseHelper(this,"CREATE_USER.db" ,null,1);
SQLiteDatabase db=dbhelper.getReadableDatabase();
Cursor cursor=db.rawQuery("select * " + "from user where name=?",new String[]{Name.getText().toString()});
User user=null; if(cursor.moveToFirst()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password =cursor.getString(cursor.getColumnIndex("password"));
user=new User(id,name,password);
cursor.close();
db.close(); if(user.getPassword().equals(Password.getText().toString())) {
TextView tv = new TextView(this);
tv.setText(user.toString());
tv.setTextSize(20);
Layout.addView(tv);
}
else if (Password.getText().toString().length()==0){
Toast.makeText(MainActivity.this,"用户不存在",Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MainActivity.this,"密码不正确",Toast.LENGTH_SHORT).show();
}}
break;
}}}
MyDatabaseHelper.java

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public static final String CREATE_USER = "create table User("
+"id integer primary key autoincrement,"
+"name text not null,"
+"password integer not null)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context , name ,factory,version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_USER);
Toast.makeText(mContext, "", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL("drop table if exists User");
onCreate(db);
}
}
User.java
public class User {
private String name;
private String password;
private int id; public User(int id,String name,String password){
super();
this.id = id;
this.name = name;
this.password = password;
}
public void setId(int id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public void setPassword(String password){
this.password = password;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public String toString() {
return "Id:"+this.id+" 姓名:"+this.name+" 密码:"+this.password;
}
}
												

最新文章

  1. apache启动出错原因举例
  2. canvas判断边距,反弹和拖拽的综合实例
  3. Android requires compiler compliance level 5.0 or 6.0. Found &#39;1.7&#39; instead. Please use Android Tool
  4. html 特殊字符 fmt table A
  5. 图算法(一)——基本图算法(BFS,DFS及其应用)(2)
  6. WP8&mdash;&mdash;页面跳转方法
  7. Ubuntu开机出现:Fontconfig warning:&quot;/etc/fonts/conf.d/65-droid-sans-fonts.conf&quot;的解决办法
  8. ARM和X86功耗差别的深层原因探讨
  9. Cordys BOP 4平台开发入门实战演练——Webservices开发(0基础)
  10. 16.怎样自学Struts2之Struts2异常处理[视频]
  11. 关于mysql中触发器old和new如何更好的区别我有话要说?
  12. BM算法学习笔记
  13. HTML 5 拖放
  14. 微信小程序开发3之保存数据及页面跳转
  15. PHP 5.6 开启CURL HTTPS 类型
  16. C语言中的指针和内存泄漏几种情况
  17. 【TensorFlow】Python解析xml文件
  18. APP流氓大法之apk 静默安装
  19. S/4HANA生产订单增强WORKORDER_UPDATE方法BEFORE_UPDATE参数分析
  20. redis集群步骤(windows环境)

热门文章

  1. sqlserver创建接受任何类型的nvl
  2. sqlserver从xlsx读取数据
  3. 核心思想:许多公司都没有认识到云储存的革命性(类似QQ把它搞成了用户的家、再也离不开了)
  4. 重写QLineEdit,实现编辑框内添加删除按钮的功能(随时把控件Move到一个地方,然后show就可以了,这是万能的办法)
  5. 宿主机与虚拟机系统的USB设备切换
  6. c# 文本超长截断
  7. SQLite实现内存键值存储
  8. LOG4NET图文教程
  9. Linux编辑器Vim和Emacs入门
  10. QList使用下标[index]才可以获得可修改的item的引用(估计QStringList也是如此)