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();
&n