项目简介:做一个登录注册界面,点击登录和注册可以进行相应的操作。示例图如下:
接下来说一下具体的代码:
1.MainActivity.java
public class MainActivity extends Activity {
private EditText edname;
private EditText edpassword;
private Button btregister;
private Button btlogin;
public static SQLiteDatabase db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
edname=(EditText)findViewById(R.id.edname);
edpassword=(EditText)findViewById(R.id.edpassword);
btlogin=(Button)findViewById(R.id.btlogin);
btregister=(Button)findViewById(R.id.btregister);
db=SQLiteDatabase.openOrCreateDatabase(MainActivity.this.getFilesDir().toString()+"/test."
+ "dbs",null);
btregister.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this, RegistersActivity.class);
startActivity(intent);
}
});
btlogin.setOnClickListener(new LoginListener());
}
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
db.close();
}
class LoginListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = edname.getText().toString();
String password = edpassword.getText().toString();
if (name.equals("") || password.equals("")) {
// 弹出消息框
new AlertDialog.Builder(MainActivity.this).setTitle("错误")
.setMessage("帐号或密码不能空").setPositiveButton("确定", null)
.show();
} else {
isUserinfo(name, password);
}
}
// 判断输入的用户是否正确
public Boolean isUserinfo(String name, String pwd) {
try{
String str="select * from tb_user where name=? and password=?";
Cursor cursor = db.rawQuery(str, new String []{name,pwd});
if(cursor.getCount()<=0){
new AlertDialog.Builder(MainActivity.this).setTitle("错误")
.setMessage("帐号或密码错误!").setPositiveButton("确定", null)
.show();
return false;
}else{
new AlertDialog.Builder(MainActivity.this).setTitle("正确")
.setMessage("成功登录").setPositiveButton("确定", null)
.show();
return true;
}
}catch(SQLiteException e){
createDb();
}
return false;
}
}
// 创建数据库和用户表
public void createDb() {
db.execSQL("create table tb_user( name varchar(30) primary key,password varchar(30))");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
2.RegistersActivity.java
public class RegistersActivity extends Activity {
private EditText edname1;
private EditText edpassword1;
private Button btregister2;
private Button btreset;
SQLiteDatabase db;
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
db.close();
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
edname1 = (EditText) findViewById(R.id.edname1);
edpassword1 = (EditText) findViewById(R.id.edpassword1);
btregister2 = (Button) findViewById(R.id.btregister2);
btreset=(Button) findViewById(R.id.btreset);
btreset.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
edname1.setText("");
edpassword1.setText("");
}
});
btregister2.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String name = edname1.getText().toString();
String password = edpassword1.getText().toString();
if (!(name.equals("") && password.equals(""))) {
if (addUser(name, password)) {
DialogInterface.OnClickListener ss = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
// TODO Auto-generated method stub
// 跳转到登录界面
Intent in = new Intent();
in.setClass(RegistersActivity.this,MainActivity.class);
startActivity(in);
// 销毁当前activity
RegistersActivity.this.onDestroy();
}
};
new AlertDialog.Builder(RegistersActivity.this)
.setTitle("注册成功").setMessage("注册成功")
.setPositiveButton("确定", ss).show();
} else {
new AlertDialog.Builder(RegistersActivity.this)
.setTitle("注册失败").setMessage("注册失败")
.setPositiveButton("确定", null);
}
} else {
new AlertDialog.Builder(RegistersActivity.this)
.setTitle("帐号密码不能为空").setMessage("帐号密码不能为空")
.setPositiveButton("确定", null);
}
}
});
}
//添加用户
public Boolean addUser(String name, String password) {
String str = "insert into tb_user values(?,?) ";
MainActivity main = new MainActivity();
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()
+ "/test.dbs", null);
main.db = db;
try {
db.execSQL(str, new String[] { name, password });
return true;
} catch (Exception e) {
main.createDb();
}
return false;
}
}
3.login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/_002"
tools:context=".MainActivity" >
<TextView
android:id="@+id/login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center_horizontal"
android:textColor="#ff0000"
android:textSize="35dp"
android:text="登录" />
<TextView
android:id="@+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/login"
android:layout_marginRight="5dp"
android:layout_marginBottom="30dp"
android:textSize="28dp"
android:text="帐号:"/>
<EditText
android:id="@+id/edname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_below="@id/login"
android:layout_toRightOf="@id/txtname"
android:layout_alignParentRight="true"
android:hint="请输入用户帐号"/>
<TextView
android:id="@+id/txtpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtname"
android:layout_marginRight="5dp"
android:textSize="28dp"
android:text="密码:"/>
<EditText
android:id="@+id/edpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/edname"
android:layout_toRightOf="@id/txtpassword"
android:layout_alignParentRight="true"
android:inputType="textPassword"
android:hint="请输入用户密码"/>
<TextView
android:id="@+id/txtname1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt1"
android:layout_marginRight="5dp"
android:layout_marginBottom="30dp"
android:textSize="28dp"
android:text="帐号:"/>
<EditText
android:id="@+id/edname1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_below="@id/txt1"
android:layout_toRightOf="@id/txtname1"
android:layout_alignParentRight="true"
android:hint="请输入用户帐号"/>
<LinearLayout
android:layout_below="@id/edpassword"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btlogin"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="登录"/>
<Button
android:id="@+id/btregister"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginRight="20dp"
android:text="注册"/>
</LinearLayout>
</RelativeLayout>
4.register.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/_002"
tools:context=".RegistersActivity" >
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center_horizontal"
android:textColor="#ff0000"
android:textSize="35dp"
android:text="注册" />
<TextView
android:id="@+id/txtname1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txt1"
android:layout_marginRight="5dp"
android:layout_marginBottom="30dp"
android:textSize="28dp"
android:text="帐号:"/>
<EditText
android:id="@+id/edname1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_below="@id/txt1"
android:layout_toRightOf="@id/txtname1"
android:layout_alignParentRight="true"
android:hint="请输入用户帐号"/>
<TextView
android:id="@+id/txtpassword1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtname1"
android:layout_marginRight="5dp"
android:textSize="28dp"
android:text="密码:"/>
<EditText
android:id="@+id/edpassword1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/txtname1"
android:layout_toRightOf="@id/txtpassword1"
android:layout_alignParentRight="true"
android:inputType="textPassword"
android:hint="请输入用户密码"/>
<LinearLayout
android:layout_below="@id/txtpassword1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_horizontal" >
<Button
android:id="@+id/btregister2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="提交"/>
<Button
android:id="@+id/btreset"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginRight="20dp"
android:text="重置"/>
</LinearLayout>
</RelativeLayout>
下面对上述内容做简要说明:
a.对于题目内容,我们可以这么理解:需要做两个页面,一个用来选择登录还是注册,另一个页面则是注册个人信息。那么就需要两个Activity来对两个xml文件所做的布局进行具体的操作设置。
b.有六种布局,大家都可以尝试一下,上面那种是相对布局,根据每个组件的相对位置进行布局。(android:layout_below=“ ” ;//在哪个组件的下面,
android:layout_toRightOf=“ ”;//在哪个组件的右面)
c.对于页面之间的跳转,需要在这两处进行设置:(1).在Activity中添加监听事件( 例如: btregister.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this, RegistersActivity.class);
startActivity(intent);
}); )
(2).在manifest.xml中设置跳转,如下图(在红色方框的位置进行设置要跳转的页面):
对于代码和图片的放置位置如下图: