android登录注册页面的简单实现
页面实现如上图。
点击注册新用户按钮能跳转到注册页面
点击注册 注册成功跳转到登录页面。
代码如下
首先是注册页面的布局,我用了几个线性布局
-
<TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:text="用户名:" /> <EditText android:id="@+id/eUsername" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/tv1" android:background="#D3D3D3" android:maxLength="16" android:minEms="10" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:id="@+id/layout2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/layout1" android:orientation="vertical" > <TextView android:id="@+id/tv2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" /> <EditText android:id="@+id/ePassword" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/tv2" android:background="#D3D3D3" android:inputType="textPassword" android:maxLength="16" android:minEms="10" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:id="@+id/layout3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/layout2" android:orientation="vertical" > <TextView android:id="@+id/tv3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确 认:" /> <EditText android:id="@+id/ePassagain" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/ePassagain" android:background="#D3D3D3" android:inputType="textPassword" android:maxLength="16" android:minEms="10" > <requestFocus /> </EditText> </LinearLayout>
性别这一块用了RadioButton
<TextView
android:id="@+id/tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性 别:" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<RadioGroup
android:id="@+id/rgsex"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rdMan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男" />
<RadioButton
android:id="@+id/rdwem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="女" />
</RadioGroup>
</LinearLayout>
</LinearLayout>
其他那些下拉列表就不展示了,最后是注册按钮
<Button
android:id="@+id/zc"
android:layout_width="296dp"
android:layout_height="wrap_content"
android:onClick="doClick"
android:text="注 册" />
</LinearLayout>
以上就是注册页面的布局。
接下来是祖册页面的Activity的代码。
首先要定义以及初始化
EditText eUsername, ePassword, ePassagain, n;
RadioGroup rgsex;
CheckBox cb1, cb2, cb3;
StringBuilder sb = new StringBuilder("");
eUsername = (EditText) findViewById(R.id.eUsername);
ePassword = (EditText) findViewById(R.id.ePassword);
ePassagain = (EditText) findViewById(R.id.ePassagain);
n = (EditText) findViewById(R.id.n);
rgsex = (RadioGroup) findViewById(R.id.rgsex);
cb1 = (CheckBox) findViewById(R.id.cb1);
cb2 = (CheckBox) findViewById(R.id.cb2);
cb3 = (CheckBox) findViewById(R.id.cb3);
我主要是在按钮上设置了DoClick,之后我就在activity。设置doclick()方法。
public void doClick(View v) {
switch (v.getId()) {
case R.id.zc:
if (!eUsername.getText().toString().trim().equals("")
&& !ePassword.getText().toString().trim().equals("")) {
if (ePassword.getText().toString()
.equals(ePassagain.getText().toString())) {
StringBuilder sb = new StringBuilder(“注册信息如下:\n”);
sb.append("用户名:").append(eUsername.getText().toString())
.append('\n');
sb.append("密码:").append(ePassword.getText().toString())
.append('\n');
break;
switch (rgsex.getCheckedRadioButtonId()) {
case R.id.rdMan:
sb.append("性别: 男\n");
break;
case R.id.rdwem:
sb.append("性别: 女\n");
break;
}
Intent intent = new Intent(MainActivity.this,
DLActivity.class);
intent.putExtra("regInfo", sb.toString());
startActivity(intent);
finish();
String regInfo = intent.getStringExtra("regInfo");
Toast.makeText(MainActivity.this, "注册信息为" + regInfo, 5000)
.show();
} else {
Toast.makeText(this, "两次密码不一致,请重新输入", 3000).show();
}
} else {
Toast.makeText(this, "账号和密码不能为空", 3000).show();
}
break;
}
}
这就是简单的注册页面。