1)在/res/layout中创建login.xml(活动布局)在这个例子中,有一个额外的密码更改选项
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/username"
android:hint="Username"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:layout_gravity="center"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/password"
android:layout_width="250dp"
android:hint="Password"
android:layout_marginTop="20dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/Loginbutton"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login" />
<CheckedTextView
android:id="@+id/changepassword"
android:layout_width="wrap_content"
android:layout_marginTop="75dp"
android:clickable="true"
android:layout_marginLeft="190dp"
android:layout_height="wrap_content"
android:text="Change_Login_Id" />
</LinearLayout>
2) 在 /src// 中创建 Login.java 类
public class Login extends Activity{
private Button login;
private EditText Username;
private EditText Password;
private CheckedTextView changeid;
public SQLiteDatabase sampleDB;
public String COLUMN_ID="_id";
public String COLUMN1="username";
public String COLUMN2="password";
public String TABLE_NAME="Androdata";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
login=(Button)findViewById(R.id.Loginbutton);
Username=(EditText)findViewById(R.id.username);
Password=(EditText)findViewById(R.id.password);
changeid=(CheckedTextView)findViewById(R.id.changepassword);
sampleDB = this.openOrCreateDatabase(TABLE_NAME, MODE_PRIVATE, null);
boolean x=init(TABLE_NAME);
if(x==false)
{
createDB();
insertDB();
}
changeid.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myintent=new Intent("android.intent.action.DATABASE");
startActivity(myintent);
}
});
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int k=check();
if(k==1)
{
Toast.makeText(Login.this, "Login Successful ...", Toast.LENGTH_SHORT).show();
Intent myintent=new Intent("android.intent.action.CHOICE");
startActivity(myintent);
}
else
{
Username.setText("");
Password.setText("");
Toast.makeText(Login.this, "Authentication Failed...", Toast.LENGTH_SHORT).show();
}
}
});
}
public boolean init(String tableName)
{
Cursor cursor = sampleDB.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+tableName+"'", null);
if(cursor!=null) {
if(cursor.getCount()>0) {
cursor.close();
return true;
}
cursor.close();
}
return false;
}
private void insertDB() {
sampleDB.execSQL("INSERT INTO " +
TABLE_NAME +
" Values ('1','Androviewer','viewer');");
System.out.println("Inserted data successfully....");
}
private void createDB() {
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
TABLE_NAME+ "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN1
+ " text not null,"+ COLUMN2
+ " text not null);");
System.out.println("Database created successfully....");
}
private int check() {
String a=Username.getText().toString();
String b=Password.getText().toString();
// TODO Auto-generated method stub
Cursor c = sampleDB.rawQuery("SELECT username, password FROM " +
TABLE_NAME +
" where _id=1", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String orgusername = c.getString(c.getColumnIndex("username"));
String orgpassword = c.getString(c.getColumnIndex("password"));
if(a.equals(orgusername)&&b.equals(orgpassword))
{
return 1;
}
else
{
return 0;
}
}while (c.moveToNext());
}
}
return 0;
}
}