您的位置:首页 > 其它

聊天登陆之注册界面

2015-06-08 18:36 393 查看
安卓聊天软件的第一步,写好注册界面。

这里的注册界面我用到无网络型的数据库来进行增加查询用户名称及密码。

下面是注册界面的代码:

<span style="font-size:24px;">public class Register extends Activity {

private RegisterOpenHelper reHelper;
private RegisterDB registerDB;
@SuppressWarnings("unused")
private String UserName,UserPassword,UserRepassword;
private User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_register);
reHelper = new RegisterOpenHelper(this, "Users.db", null, 1);
Button tijiao = (Button) findViewById(R.id.tijiao);

tijiao.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Register(v);
}
});

}
private void Register(View v){
SQLiteDatabase db = reHelper.getWritableDatabase();
ContentValues values = new ContentValues();
//获取EditText写入的信息
String userName = ((EditText)findViewById(R.id.register_username)).getText().toString();
String userPassword = ((EditText) findViewById(R.id.register_password)).getText().toString();
String userRepassword = ((EditText)findViewById(R.id.register_confirm)).getText().toString();
Cursor cursor = db.query("user", null, null, null, null, null, null);
user = new User();
if(cursor.moveToFirst()){//遍历查询信息
do{
UserName = cursor.getString(cursor.getColumnIndex("user_name"));
UserPassword = cursor.getString(cursor.getColumnIndex("user_password"));
UserRepassword = cursor.getString(cursor.getColumnIndex("user_repassword"));
}while(cursor.moveToNext());
}
cursor.close();
if (userPassword.equals(userRepassword)) {
if (!userPassword.isEmpty()) {
if (!userName.isEmpty()) {
if (!UserName.equals(userName)) {
values.put("user_name", userName);
values.put("user_password",userPassword);
values.put("user_repassword", userRepassword);
db.insert("user", null, values);
Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Register.this,Main.class);
startActivity(intent);
return;
} else {
Toast.makeText(Register.this, "该用户已被注册", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(Register.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(Register.this, "密码不能为空", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(Register.this, "请再次确认密码", Toast.LENGTH_SHORT).show();
}
}

}

</span>

接下来是注册界面的XML代码:

<span style="font-size:24px;"><?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="24dip"
android:background="@color/cell_header_bg"
android:gravity="center_vertical"
android:paddingLeft="15dip"
android:text="@string/str_input_necessary"
android:textColor="@color/black"
android:textSize="12sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="52dip"
android:background="@drawable/contact_list_bg_selector"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="80dip"
android:layout_height="fill_parent"
android:layout_marginLeft="15dip"
android:gravity="center_vertical"
android:text="@string/str_register_username"
android:textColor="@color/black"
android:textSize="16sp" />

<EditText
android:id="@+id/register_username"
style="@style/register_editbox"
android:ellipsize="end"
android:hint="@string/str_register_username_hint" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="52dip"
android:background="@drawable/contact_list_bg_selector"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="80dip"
android:layout_height="fill_parent"
android:layout_marginLeft="15dip"
android:gravity="center_vertical"
android:text="@string/str_register_password"
android:textColor="@color/black"
android:textSize="16sp" />

<EditText
android:id="@+id/register_password"
style="@style/register_editbox"
android:ellipsize="end"
android:hint="@string/str_register_password_hint"
android:password="true" />
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="52dip"
android:background="@drawable/contact_list_bg_selector"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="80dip"
android:layout_height="fill_parent"
android:layout_marginLeft="15dip"
android:gravity="center_vertical"
android:text="@string/str_register_confirm"
android:textColor="@color/black"
android:textSize="16sp" />

<EditText
android:id="@+id/register_confirm"
style="@style/register_editbox"
android:ellipsize="end"
android:hint="@string/str_register_confirm_hint"
android:inputType="textPassword"
android:password="true" />
</LinearLayout>

<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="52dip"
android:background="@drawable/contact_list_bg_selector" >
<Button
android:id="@+id/tijiao"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_x="100dp"
android:layout_y="0dp"
android:text="提交" />
</AbsoluteLayout>
</LinearLayout>

</ScrollView></span>
然后我们来看一下主页面,这里登陆成功后会跳转到注册界面,如需更改跳转后的界面,可将代码:
<span style="font-size:24px;">butregister.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
finish();
Intent intent = new Intent(Main.this,Register.class);
startActivity(intent);
}
});</span>
中的Register.class更换为所需界面 (名称.class)

主页面代码:

<span style="font-size:24px;">public class Main extends Activity {

private RegisterOpenHelper reHelper;
private String userPasswordString;
private User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
reHelper = new RegisterOpenHelper(this, "Users.db", null, 1);
Button butregister = (Button)findViewById(R.id.butregister);
Button butlogin = (Button)findViewById(R.id.butlogin);
butregister.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
finish();
Intent intent = new Intent(Main.this,Register.class);
startActivity(intent);
}
});
butlogin.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Login(v);
}
});
}
private void Login(View v){
SQLiteDatabase db = reHelper.getWritableDatabase();
String userName = ((EditText)findViewById(R.id.register_username)).getText().toString();
String userPassword = ((EditText) findViewById(R.id.register_password)).getText().toString();
Cursor cursor = db.query("user", null, null, null, null, null, null);
user = new User();
if(cursor.moveToFirst()){
do{
String UserName = cursor.getString(cursor.getColumnIndex("user_name"));
String UserPassword = cursor.getString(cursor.getColumnIndex("user_password"));
String UserRepassword = cursor.getString(cursor.getColumnIndex("user_repassword"));
userPasswordString = UserPassword;
}while(cursor.moveToNext());
}
cursor.close();

if((userPassword.compareTo(userPasswordString))==0){
finish();
Toast.makeText(Main.this, "登陆成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Main.this,Register.class);
startActivity(intent);
}

}

}
</span>
主页面XML代码:

<span style="font-size:24px;"><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.chat.Main" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="52dip"
android:background="@drawable/contact_list_bg_selector"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="80dip"
android:layout_height="fill_parent"
android:layout_marginLeft="15dip"
android:gravity="center_vertical"
android:text="@string/str_register_username"
android:textColor="@color/black"
android:textSize="16sp" />

<EditText
android:id="@+id/register_username"
style="@style/register_editbox"
android:ellipsize="end"
android:hint="@string/str_register_username_hint" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="52dip"
android:background="@drawable/contact_list_bg_selector"
android:gravity="center_vertical"
android:orientation="horizontal" >

<TextView
android:layout_width="80dip"
android:layout_height="fill_parent"
android:layout_marginLeft="15dip"
android:gravity="center_vertical"
android:text="@string/str_register_password"
android:textColor="@color/black"
android:textSize="16sp" />

<EditText
android:id="@+id/register_password"
style="@style/register_editbox"
android:ellipsize="end"
android:hint="@string/str_register_password_hint"
android:password="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="52dip"
android:background="@drawable/contact_list_bg_selector" >
<Button
android:id="@+id/butregister"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="注册" />
<Button
android:id="@+id/butlogin"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>

</TableLayout>
</span>
在这里我们用到数据库。

数据库建表代码:

<span style="font-size:24px;">public static final String CREATE_USER = "create table user("
+ "id integer primary key autoincrement,"
+ "user_name text,"
+ "user_password text,"
+ "user_repassword text)";</span>
代码链接下载:http://download.csdn.net/download/lady_zhou/8786513
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: