1.登录
1.1 类文件
public class LoginActivity extends Activity{
EditText username;
EditText password;
Button login,register;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_first);
findViews();
}
private void findViews() {
username=(EditText) findViewById(R.id.username);
password=(EditText) findViewById(R.id.password);
login=(Button) findViewById(R.id.login);
register=(Button) findViewById(R.id.register);
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String name=username.getText().toString();
String pass=password.getText().toString();
Log.i("TAG",name+"_"+pass);
UserService uService=new UserService(LoginActivity.this);
boolean flag=uService.login(name, pass);
if(flag){
Log.i("TAG","登录成功");
Toast.makeText(LoginActivity.this, "登录成功",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(LoginActivity.this,
PersonalActivity.class);
startActivity(intent);
}else{
Log.i("TAG","登录失败");
Toast.makeText(LoginActivity.this, "登录失败",
Toast.LENGTH_LONG).show();
}
}
});
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent(LoginActivity.this,
RegisterActivity.class);
startActivity(intent);}
});}
}
1.2 结构文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="169dp"
android:orientation="vertical" >
<Button
android:id="@+id/btn_home"
android:layout_width="40dp"
android:layout_height="36dp"
android:layout_marginTop="13dp"
android:background="@mipmap/cb_icon_discover_selected"
android:contentDescription="@string/app_name" />
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="@string/app_name"
android:src="@mipmap/cb_icon_more_selected" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageview"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:text="@string/please_login_text"
android:textColor="@color/base_text_color"
android:textSize="@dimen/txt_h3" />
</RelativeLayout>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="用户名:"
android:textColor="@color/white"/>
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="密码:"
android:textColor="@color/white" />
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
</TableRow>
<TableRow >
<TextView />
<LinearLayout >
<Button
android:id="@+id/login"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="登录" />
<Button
android:id="@+id/register"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
2.注册
2.1 类文件
public class RegisterActivity extends Activity {
EditText username;
EditText password;
EditText age;
RadioGroup sex;
Button register;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
findViews();
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String name=username.getText().toString().trim();
String pass=password.getText().toString().trim();
String agestr=age.getText().toString().trim();
String sexstr=((RadioButton)RegisterActivity.
this.findViewById(
sex.getCheckedRadioButtonId())).getText().toString();
Log.i("TAG",name+"_"+pass+"_"+agestr+"_"+sexstr);
UserService uService=new UserService(RegisterActivity.this);
User user=new User();
user.setUsername(name);
user.setPassword(pass);
user.setAge(Integer.parseInt(agestr));
user.setSex(sexstr);
uService.register(user);
Toast.makeText(RegisterActivity.this, "注册成功",
Toast.LENGTH_LONG).show();
}
});}
private void findViews() {
username=(EditText) findViewById(R.id.usernameRegister);
password=(EditText) findViewById(R.id.passwordRegister);
age=(EditText) findViewById(R.id.ageRegister);
sex=(RadioGroup) findViewById(R.id.sexRegister);
register=(Button) findViewById(R.id.Register);}
}
2.2 结构文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="91dp"
android:layout_gravity="center"
android:gravity="center"
android:text="注册"
android:textSize="28dp"
android:textStyle="bold" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="用户名:"
android:textColor="@color/white" />
<EditText
android:id="@+id/usernameRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="密码:"
android:textColor="@color/white" />
<EditText
android:id="@+id/passwordRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="年龄:"
android:textColor="@color/white" />
<EditText
android:id="@+id/ageRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入年龄" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="性别:"
android:textColor="@color/white"/>
<RadioGroup
android:id="@+id/sexRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/woman"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/nan"
android:text="男"
android:textColor="@color/white" />
<RadioButton
android:id="@+id/women"
android:text="女"
android:textColor="@color/white" />
</RadioGroup>
</TableRow>
<TableRow >
<TextView />
<LinearLayout >
<Button
android:id="@+id/Register"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="注册" />
</LinearLayout>
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.97"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout14"
android:layout_width="match_parent"
android:layout_height="26dp"
android:background="@color/color_nav_text"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back" />
<LinearLayout
android:id="@+id/linearLayout16"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.18"
android:orientation="vertical" >
</LinearLayout>
<ImageView
android:id="@+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home" />
<LinearLayout
android:id="@+id/linearLayout17"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.17"
android:orientation="vertical" >
</LinearLayout>
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search" />
</LinearLayout>
</LinearLayout>