zoukankan      html  css  js  c++  java
  • 团队计划(5.19)

    这是用心做的一天,博客内容比较啰嗦,不过能让初学android和adt的同学收获很多。

    今天做了什么?

    成功实践了登陆注册功能。

    源代码:MainActivity 数据库的声明

     1 package com.example.app05;
     2 
     3 import android.content.Context;
     4 import android.database.sqlite.SQLiteDatabase;
     5 import android.database.sqlite.SQLiteOpenHelper;
     6 import android.util.Log;
     7 
     8 /*
     9  * 
    10  *实现数据库openhelper
    11  *
    12  *
    13 */
    14 public class MainActivity extends SQLiteOpenHelper
    15 {
    16     final static String DATABASENAME = "my_database.db";
    17     final static int    VERSION      = 1;
    18     final static String TABLENAME    = "userInfor";
    19     final static String ID           = "id";
    20     
    21     final static String NAME         = "name";
    22     final static String SEX          = "sex";
    23     final static String AGE          = "age";
    24     final static String HOBBY        = "hobby";
    25     final static String PASSWORD     = "password";
    26     
    27     public static final String TAG = "MY_TAG";//日志
    28     
    29     public MainActivity(Context context)
    30     {
    31         super(context,DATABASENAME,null,VERSION);
    32     }
    33     
    34     
    35     public void onCreate(SQLiteDatabase db)
    36     {
    37         Log.v(TAG,"王大思的标记1.1");
    38         
    39         String sql2 = "CREATE TABLE " +
    40         TABLENAME + "(" +
    41         ID        + " INTEGER PRIMARY KEY AUTOINCREMENT," +
    42         NAME      + " TEXT," +
    43         PASSWORD  + " TEXT," +
    44         SEX       + " TEXT," +
    45         AGE       + " TEXT," +
    46         HOBBY     + " TEXT);";
    47         Log.v(TAG,"王大思的标记1.2");
    48         db.execSQL(sql2);
    49         Log.v(TAG,"王大思的标记1.3");
    50     }
    51     
    52     public void onUpgrade(SQLiteDatabase arg0,int arg1,int arg2)
    53     {    
    54         Log.v(TAG,"王大思的标记1.3");
    55     }
    56 }

    MainActivity02

    登陆界面

    实现功能:检测用户输入的用户名是否存在,不存在就提示,存在就比对密码。

     1 package com.example.app05;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.database.Cursor;
     6 import android.database.sqlite.SQLiteDatabase;
     7 import android.os.Bundle;
     8 import android.util.Log;
     9 import android.view.View;
    10 import android.view.View.OnClickListener;
    11 import android.widget.Button;
    12 import android.widget.EditText;
    13 import android.widget.Toast;
    14 
    15 /*
    16  * 
    17  *登陆界面的实现
    18  *
    19  *
    20 */
    21 
    22 public class MainActivity02 extends Activity {
    23     String name;
    24     String pass;
    25     String sex;
    26     String age;
    27     String hobby;
    28     SQLiteDatabase db;
    29     
    30     public void onCreate(Bundle savedInstanceState)
    31     {
    32         super.onCreate(savedInstanceState);
    33         setContentView(R.layout.activity_main);//登陆界面的连接
    34         
    35         Button loginBtu = (Button) findViewById(R.id.button0);
    36         Button regBtu = (Button) findViewById(R.id.button1);
    37         Log.v("TAG","王大思的标记2.1");
    38         final EditText et1 = (EditText) findViewById(R.id.Name_in);
    39         final EditText et2 = (EditText) findViewById(R.id.Pass_in);
    40         
    41         regBtu.setOnClickListener(new OnClickListener()
    42         {
    43             public void onClick(View arg0)
    44             {
    45                 Intent i =new Intent(MainActivity02.this,MainActivity03.class);
    46                 startActivity(i);
    47             }
    48         });
    49         Log.v("TAG","王大思的标记2.2");
    50         loginBtu.setOnClickListener(new OnClickListener()
    51         {
    52             public void onClick(View v)
    53             {
    54                 MainActivity helper = new MainActivity(getBaseContext());
    55                 db = helper.getReadableDatabase();
    56                 Log.v("TAG","王大思的标记2.3");
    57                 //获得参数
    58                 name = et1.getText().toString();
    59                 pass = et2.getText().toString();
    60                 
    61                 //根据用户名查询数据库信息
    62                 Cursor cursor = db.query(MainActivity.TABLENAME, new String[]{MainActivity.PASSWORD}, MainActivity.NAME + "=?", new String[]{name},null,null,null);
    63                 Log.v("TAG","王大思的标记2.4");
    64                 //不存在那么提示不存在,不再继续
    65                 if(cursor.getCount() == 0)
    66                 {
    67                     Toast.makeText(getBaseContext(), "该用户不存在!!", Toast.LENGTH_LONG).show();
    68                     return;
    69                 }
    70                 Log.v("TAG","王大思的标记2.5");
    71                 //如果存在,就继续
    72                 cursor.moveToFirst();
    73                 String password = cursor.getString(0);
    74                 Log.v("TAG","王大思的标记2.6");
    75                 //判断密码,如果一样就跳转,否则提示密码错误
    76                 if(password.equals(pass))
    77                 {
    78                     Log.v("TAG","王大思的标记2.7");
    79                     Intent i = new Intent(MainActivity02.this,Mainactivity04.class);
    80                     i.putExtra("name", name);
    81                     startActivity(i);
    82                 }
    83                 
    84                 else
    85                 {
    86                     Toast.makeText(getBaseContext(), "密码错误!!", Toast.LENGTH_LONG).show();
    87                 }
    88                 
    89             }
    90         });
    91     }
    92 }

    MainActivity03

    注册界面

    功能实现:根据用户输入的信息,判断是否已经存在,不存在就存入数据库。

     1 package com.example.app05;
     2 
     3 import android.app.Activity;
     4 import android.content.ContentValues;
     5 import android.content.Intent;
     6 import android.database.Cursor;
     7 import android.database.sqlite.SQLiteDatabase;
     8 import android.os.Bundle;
     9 import android.util.Log;
    10 import android.view.View;
    11 import android.view.View.OnClickListener;
    12 import android.widget.Button;
    13 import android.widget.EditText;
    14 import android.widget.Toast;
    15 /*
    16  * 
    17  *注册界面的实现
    18  *
    19  *
    20 */
    21 public class MainActivity03 extends Activity{
    22     
    23     String name;
    24     String pass;
    25     String sex;
    26     String age;
    27     String hobby;
    28     SQLiteDatabase db;
    29     
    30     public void onCreate(Bundle savedInstanceState)
    31     {
    32         Log.v("TAG","王大思的标记3.1");
    33         super.onCreate(savedInstanceState);
    34         setContentView(R.layout.activity_main02);//注册界面的连接
    35         Log.v("TAG","王大思的标记3.2");
    36         final EditText et1 =(EditText) findViewById(R.id.Name_in);
    37         final EditText et2 =(EditText) findViewById(R.id.Pass_in);
    38         final EditText et3 =(EditText) findViewById(R.id.Sex_in);
    39         final EditText et4 =(EditText) findViewById(R.id.Age_in);
    40         final EditText et5 =(EditText) findViewById(R.id.Hobby_in);
    41         Button okBtn = (Button) findViewById(R.id.okBtn);
    42         Log.v("TAG","王大思的标记3.3");
    43         okBtn.setOnClickListener(new OnClickListener()
    44         {
    45             public void onClick(View arg0)
    46             {
    47                 //得到用户信息
    48                 name = et1.getText().toString();
    49                 pass = et2.getText().toString();
    50                 sex = et3.getText().toString();
    51                 age = et4.getText().toString();
    52                 hobby = et5.getText().toString();
    53                 //得到数据库对象
    54                 Log.v("TAG","王大思的标记3.4");
    55                 MainActivity helper = new MainActivity(getBaseContext());
    56                 db = helper.getWritableDatabase();
    57                 //判断用户名是否已经存在
    58                 Cursor cursor = db.query(MainActivity.TABLENAME, new String[]{MainActivity.NAME}, MainActivity.NAME + "=?", new String[]{name},null,null,null);
    59                 //如果查询到记录则提示“已存在”
    60                 Log.v("TAG","王大思的标记3.5");
    61                 if(cursor.getCount()>0)
    62                 {
    63                     Toast.makeText(getBaseContext(), "该用户已存在!!",Toast.LENGTH_LONG).show();
    64                     return;
    65                 }
    66                 Log.v("TAG","王大思的标记3.6");
    67                 //不存在则插入数据
    68                 ContentValues values =new ContentValues();
    69                 values.put(MainActivity.NAME, name);
    70                 values.put(MainActivity.PASSWORD, pass);
    71                 values.put(MainActivity.SEX, sex);
    72                 values.put(MainActivity.AGE, age);
    73                 values.put(MainActivity.HOBBY, hobby);
    74                 db.insert(MainActivity.TABLENAME, null, values);
    75                 //进行跳转,将参数传递给登陆成功界面
    76                 Intent i = new Intent(MainActivity03.this,Mainactivity04.class);
    77                 i.putExtra("name",name);
    78                 Log.v("TAG","王大思的标记3.7");
    79                 startActivity(i);
    80             }
    81         });
    82     }
    83 }

    MainActivity04

    登录成功界面

    实现功能:如果登陆成功就输出用户信息。

     1 package com.example.app05;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.database.Cursor;
     6 import android.database.sqlite.SQLiteDatabase;
     7 import android.os.Bundle;
     8 import android.util.Log;
     9 import android.view.View;
    10 import android.view.View.OnClickListener;
    11 import android.widget.Button;
    12 import android.widget.TextView;
    13 
    14 public class Mainactivity04 extends Activity{
    15     String name;
    16     String pass;
    17     String sex;
    18     String age;
    19     String hobby;
    20     
    21     SQLiteDatabase db;
    22     
    23     TextView name_in;
    24     TextView pass_in;
    25     TextView sex_in;
    26     TextView age_in;
    27     TextView hobby_in;
    28     Button btu;
    29     
    30     public void initView()
    31     {
    32             btu =(Button) findViewById(R.id.btu);
    33             name_in = (TextView) findViewById(R.id.name_in);
    34             pass_in = (TextView) findViewById(R.id.pass_in);
    35             sex_in = (TextView) findViewById(R.id.sex_in);
    36             age_in = (TextView) findViewById(R.id.age_in);
    37             hobby_in = (TextView) findViewById(R.id.hobby_in);
    38             Log.v("TAG","王大思的标记4.1");
    39             btu.setOnClickListener(new OnClickListener()
    40             {
    41                 public void onClick(View v)
    42                 {
    43                     Intent i = new Intent(Mainactivity04.this,MainActivity02.class);
    44                     startActivity(i);
    45                 }
    46             });
    47     }
    48     
    49     public void doQuery()
    50     {
    51             Log.v("TAG","王大思的标记4.2");
    52             name = getIntent().getExtras().getString("name");
    53             
    54             MainActivity helper = new MainActivity(getBaseContext());
    55             db = helper.getReadableDatabase();
    56             String[] columns =new String[]{
    57                     MainActivity.PASSWORD,
    58                     MainActivity.AGE,
    59                     MainActivity.SEX,
    60                     MainActivity.HOBBY
    61             };
    62             Log.v("TAG","王大思的标记4.3");
    63             Cursor cursor = db.query(MainActivity.TABLENAME,columns, MainActivity.NAME + "=?", new String[]{name},null,null,null);
    64             cursor.moveToFirst();
    65             while(!cursor.isAfterLast())
    66             {
    67                 pass = cursor.getString(0);
    68                 age = cursor.getString(1);
    69                 sex = cursor.getString(2);
    70                 hobby = cursor.getString(3);
    71                 cursor.moveToNext();
    72             }
    73             //显示
    74             name_in.setText(name);
    75             pass_in.setText(pass);
    76             sex_in.setText(sex);
    77             age_in.setText(age);
    78             hobby_in.setText(hobby);
    79     }
    80     
    81     public void doShow()
    82     {
    83             
    84     }
    85     
    86     public void onCreate(Bundle savedInstanceState)
    87     {
    88         Log.v("TAG","王大思的标记4.4");
    89         super.onCreate(savedInstanceState);
    90         setContentView(R.layout.activity_main03);
    91         initView();
    92         doQuery();   //进行查询
    93     }
    94 }

    布局文件:

    activity_main

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11 
    12     <TextView
    13         android:id="@+id/Name"
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:layout_above="@+id/Pass"
    17         android:layout_alignParentLeft="true"
    18         android:layout_marginBottom="71dp"
    19         android:text="帐号"
    20         android:textSize="30sp" />
    21 
    22     <TextView
    23         android:id="@+id/Pass"
    24         android:layout_width="wrap_content"
    25         android:layout_height="wrap_content"
    26         android:layout_alignLeft="@+id/Name"
    27         android:layout_centerVertical="true"
    28         android:text="密码"
    29         android:textSize="30sp" />
    30 
    31     <EditText
    32         android:id="@+id/Name_in"
    33         android:layout_width="200sp"
    34         android:layout_height="wrap_content"
    35         android:layout_alignBaseline="@+id/Name"
    36         android:layout_alignBottom="@+id/Name"
    37         android:layout_marginLeft="18dp"
    38         android:layout_toRightOf="@+id/Name"
    39         android:ems="10"
    40         android:text="请输入帐号" />
    41 
    42     <EditText
    43         android:id="@+id/Pass_in"
    44         android:layout_width="wrap_content"
    45         android:layout_height="wrap_content"
    46         android:layout_alignBottom="@+id/Pass"
    47         android:layout_alignLeft="@+id/Name_in"
    48         android:layout_alignRight="@+id/Name_in"
    49         android:ems="10"
    50         android:inputType="textPassword" />
    51 
    52     <Button
    53         android:id="@+id/button1"
    54         android:layout_width="wrap_content"
    55         android:layout_height="wrap_content"
    56         android:layout_below="@+id/Pass"
    57         android:layout_marginTop="52dp"
    58         android:layout_toRightOf="@+id/Pass"
    59         android:text="注册" />
    60 
    61     <Button
    62         android:id="@+id/button0"
    63         android:layout_width="wrap_content"
    64         android:layout_height="wrap_content"
    65         android:layout_alignBaseline="@+id/button1"
    66         android:layout_alignBottom="@+id/button1"
    67         android:layout_alignRight="@+id/Name_in"
    68         android:layout_marginRight="43dp"
    69         android:text="登录" />
    70 
    71 </RelativeLayout>

    activity_main02

      1 <?xml version="1.0" encoding="utf-8"?>
      2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
      3     android:layout_width="match_parent"
      4     android:layout_height="match_parent" >
      5 
      6     <TableRow android:gravity="center">
      7     <TextView
      8         android:id="@+id/textView1"
      9         android:layout_width="wrap_content"
     10         android:layout_height="wrap_content"
     11         android:layout_gravity="center"
     12         android:text="请输入有效信息" />
     13     </TableRow>
     14 
     15     <TableRow>
     16 
     17         <TextView
     18             android:id="@+id/Name"
     19             android:layout_width="wrap_content"
     20             android:layout_height="wrap_content"
     21             android:layout_gravity="center"
     22             android:text="用户名" />
     23 
     24         <EditText
     25             android:id="@+id/Name_in"
     26             android:layout_width="200sp"
     27             android:layout_height="wrap_content"
     28             android:ems="10" />
     29     </TableRow>
     30 
     31     <TableRow>
     32     <TextView
     33         android:id="@+id/Pass"
     34         android:layout_width="wrap_content"
     35         android:layout_height="wrap_content"
     36         android:layout_gravity="center"
     37         android:text="密码" />
     38 
     39     <EditText
     40         android:id="@+id/Pass_in"
     41         android:layout_width="wrap_content"
     42         android:layout_height="wrap_content"
     43         android:ems="10" >
     44 
     45         <requestFocus />
     46     </EditText>
     47     </TableRow>
     48     
     49     <TableRow>
     50     <TextView
     51         android:id="@+id/Age"
     52         android:layout_width="wrap_content"
     53         android:layout_height="wrap_content"
     54         android:layout_gravity="center"
     55         android:text="年龄" />
     56 
     57     <EditText
     58         android:id="@+id/Age_in"
     59         android:layout_width="wrap_content"
     60         android:layout_height="wrap_content"
     61         android:ems="10" >
     62 
     63         <requestFocus />
     64     </EditText>
     65     </TableRow>
     66     
     67     <TableRow>
     68     <TextView
     69         android:id="@+id/Sex"
     70         android:layout_width="wrap_content"
     71         android:layout_height="wrap_content"
     72         android:layout_gravity="center"
     73         android:text="性别" />
     74 
     75     <EditText
     76         android:id="@+id/Sex_in"
     77         android:layout_width="wrap_content"
     78         android:layout_height="wrap_content"
     79         android:ems="10" >
     80 
     81         <requestFocus />
     82     </EditText>
     83     
     84     </TableRow>
     85     <TableRow>
     86     <TextView
     87         android:id="@+id/Hobby"
     88         android:layout_width="wrap_content"
     89         android:layout_height="wrap_content"
     90         android:layout_gravity="center"
     91         android:text="爱好" />
     92 
     93     <EditText
     94         android:id="@+id/Hobby_in"
     95         android:layout_width="wrap_content"
     96         android:layout_height="wrap_content"
     97         android:ems="10" >
     98 
     99         <requestFocus />
    100     </EditText>
    101     </TableRow>
    102 
    103     <Button
    104         android:id="@+id/okBtn"
    105         android:layout_width="wrap_content"
    106         android:layout_height="wrap_content"
    107         android:text="确定" />
    108 
    109 </TableLayout>

    activity_main03

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:layout_gravity="center" >
     6 
     7     <TextView
     8         android:id="@+id/name_in"
     9         android:layout_width="wrap_content"
    10         android:layout_height="30sp"
    11         android:text="TextView1" />
    12 
    13     <TextView
    14         android:id="@+id/pass_in"
    15         android:layout_width="wrap_content"
    16         android:layout_height="30sp"
    17         android:text="TextView2" />
    18 
    19     <TextView
    20         android:id="@+id/sex_in"
    21         android:layout_width="wrap_content"
    22         android:layout_height="30sp"
    23         android:text="TextView3" />
    24 
    25     <TextView
    26         android:id="@+id/age_in"
    27         android:layout_width="wrap_content"
    28         android:layout_height="30sp"
    29         android:text="TextView4" />
    30 
    31     <TextView
    32         android:id="@+id/hobby_in"
    33         android:layout_width="wrap_content"
    34         android:layout_height="30sp"
    35         android:text="TextView5" />
    36 
    37     <Button
    38         android:id="@+id/btu"
    39         android:layout_width="wrap_content"
    40         android:layout_height="wrap_content"
    41         android:text="返回" />
    42 
    43 </TableLayout>

    布局文件:

    AndroidManifest.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.app05"
     4     android:versionCode="1"
     5     android:versionName="1.0" >
     6 
     7     <uses-sdk
     8         android:minSdkVersion="8"
     9         android:targetSdkVersion="18" />
    10 
    11     <application
    12         android:allowBackup="true"
    13         android:icon="@drawable/ic_launcher"
    14         android:label="@string/app_name"
    15         android:theme="@style/AppTheme" >
    16         <activity
    17             android:name="com.example.app05.MainActivity02"
    18             android:label="@string/app_name" >
    19             <intent-filter>
    20                 <action android:name="android.intent.action.MAIN" />
    21 
    22                 <category android:name="android.intent.category.LAUNCHER" />
    23             </intent-filter>
    24         </activity>
    25         
    26      
    27         
    28         <activity android:name="com.example.app05.MainActivity03"></activity>
    29         
    30         <activity android:name="com.example.app05.Mainactivity04"></activity>
    31         
    32         
    33         
    34     </application>
    35         
    36 </manifest>

    遇到的困难和需要注意的地方:

    (1)

    问题:threadid=1: thread exiting with uncaught exception (group=0xb1a91b90)

    解决方法:往往这个错误是说找不到主类,但是通常真正的错误在这句话的前两行,详细解释在这句后两行(大概)。(1) near "TABLEuserInfor": syntax error

    这是我的问题,意思就是在这个引号内部的变量旁边有语法错误,一般就是简单的语法错误,大多数是在创建表的时候缺少了空格或者逗号,我的是因为在create table后少了空格(不用形容我当时的心情了吧)

    (2)

    问题:AndroidManifest.xml配置错误

    解决方法:像我在上边贴出来的配置文件代码。需要注意两点,第一个,只要是activity就必须在AndroidManifest.xml中配置

    第二个:<category android:name="android.intent.category.LAUNCHER" />就是这句,主页就必须加上这句。

    (3)

    问题:这是个基础问题,“不知道错误在哪里”。

    解决方法:加上自己的日志标记,语句就是

    Log.v(“标记信息”,"标记内容");

    然后在logcat中找标记之间的E,红色的日志就行了。

    (4)需要使用数据库就要使用openhelper,并且在虚拟机开关后不会清空数据。

  • 相关阅读:
    00 学习资源整理
    07 MySQL的应用层调整,查询缓存设置,内存管理设置,并发参数的设置常识
    06 SQL语句编写优化
    05 Java的ReentrantLock与线程的顺序控制
    05 索引的使用常识(如何编写SQL语句避免索引失效)
    04 MYSQ的SQL优化需要了解的工具explain,profile,trace
    04 JAVA中park/unpark的原理以及JAVA在API层面线程状态总结
    03 MYSQL的体系结构以及存储引擎的基本知识
    02 链表编程题
    01 栈与队列
  • 原文地址:https://www.cnblogs.com/sisi-job/p/5505488.html
Copyright © 2011-2022 走看看