蒋彪 @ 南京 2012-12-21
1. 需求 设计
在 Android 的数据库创建一个 login 表,包括密码和账户两个字段
实现两个 activity 应用程序,其中一个,提供两个入力框,分别输入密码和账户,并且实现登录 check
登录成功之后,跳转到第二个 Activity ,在其上实现一个入力框,根据输入的参数,到 google 上检索
2. 效果图
3. 主要代码
3.1 第一个 Activity 的代码
public class Android1 extends Activity { private Button mButton1 ; private TextView mTextView1 ; private TextView mTextView2 ; private String user = new String(); private String password = new String(); private DBHelper dbHelper ;
/** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState ) { super .onCreate( savedInstanceState ); setContentView(R.layout. main );
mTextView1 = (TextView) findViewById(R.id. EditText01 ); mTextView2 = (TextView) findViewById(R.id. EditText02 ); dbHelper = new DBHelper( this , "TestDB" ); createTable();
}
public void onClick(View v) { // TODO Auto-generated method stub user = mTextView1 .getText().toString(); password = mTextView2 .getText().toString(); Boolean result = selectTable( user , password ); Log. d ( "Android1" , "the result is " + result); if (result == true ) { Intent intent = new Intent(); intent.setClass(Android1. this , Android2. class ); startActivity(intent);
} }
public void createTable() { SQLiteDatabase db = dbHelper .getWritableDatabase(); db .execSQL( "create table if not exists LOG (user varchar primary key,password varchar)" ); }
public boolean selectTable(String user, String password) { SQLiteDatabase db = dbHelper .getWritableDatabase(); try { Cursor result = db.rawQuery( "select * from LOG where user = ? AND password =?" , new String[] { user, password }); Log. d ( "Android1" , "count is " + result.getCount()); if (result.getCount() != 0) { result.close(); db.close(); return true ; } else { result.close(); db.close(); return false ; } } catch (SQLException ex) { Log. d ( "Android1" , "select table failure" ); return false ; } }
} |
3.2 第二个 Activity 的代码
public class Android2 extends Activity { private TextView mTextView1 ;
@Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout. search ); mTextView1 = (TextView) findViewById(R.id. EditTextSearch ); }
public void onClick(View v) { String key = mTextView1 .getText().toString(); Intent search = new Intent(Intent. ACTION_WEB_SEARCH );
search.addFlags(Intent. FLAG_ACTIVITY_NEW_TASK );
search.putExtra(SearchManager. QUERY , key);
final Bundle appData = getIntent().getBundleExtra( SearchManager. APP_DATA );
if (appData != null ) {
search.putExtra(SearchManager. APP_DATA , appData);
}
startActivity(search);
} } |
3.3 数据库访问控制类
public class DBHelper extends SQLiteOpenHelper {
// 数据 库 版本 private static final int VERSION = 1;
// 新建一个表
public DBHelper(Context context, String name, CursorFactory factory, int version) { super (context, name, factory, version);
}
public DBHelper(Context context, String name, int version) { this (context, name, null , version); }
public DBHelper(Context context, String name) { this (context, name, VERSION ); }
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub
} MsoNormal 发表评论
最新评论
|
评论