Android应用开发期末测验
实现QQ注册和登录功能




实现功能如下:
- 设计主界面(如图1所示),界面上的控件必须包括6个指定的控件(如图2所示);(18分)
- 在主界面上点击“注册新账号”按钮,打开注册账号界面(如图3所示),设计注册界面,必须包含7个控件;(22分)
- 在注册界面上点击“立即注册”按钮后,打开注册成功界面,将注册的信息显示在界面上;(按钮点击5分,打开界面5分,显示信息20分)
- 返回主界面进行登录,将”记住密码”复选框选中,点击登录按钮把账号和密码保存到手机中。(复选框功能10分,保存信息20分)
看似是挑战,实则找代练
看来没有大佬会了,只能靠我自己了,你们好好看哈,没办法,毕竟有点难,哈哈哈。
实现功能如下:
- 设计主界面(如图1所示),界面上的控件必须包括6个指定的控件(如图2所示);(18分)
<?xml version="1.0" encoding="utf-8"?>
<!--登录界面,用LinearLayout-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="35dp">
<ImageView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_marginLeft="20dp"
android:id="@+id/qq"
android:layout_width="wrap_content"
android:layout_marginTop="35dp"
android:text="仿QQ"
android:textSize="24sp"
android:layout_height="wrap_content" />
</LinearLayout>
<!--输入框-->
<EditText
android:id="@+id/et_user_name"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:hint="账号"
android:textSize="20sp" />
<!--输入框-->
<EditText
android:id="@+id/et_psw"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:hint="密码"
android:textSize="20sp"
android:inputType="textPassword"/>
<!--按钮-->
<Button
android:id="@+id/btn_login"
android:text="登录"
android:background="#1E90FF"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_width="320dp"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_register"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新用户注册"/>
<!--layout_weight="1" layout_width="0dp"实现均分效果-->
<TextView
android:id="@+id/tv_find_psw"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?" />
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录即代表阅读并同意服务条款" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
- 在主界面上点击“注册新账号”按钮,打开注册账号界面(如图3所示),设计注册界面,必须包含7个控件;(22分)
- public class RegisterActivity extends AppCompatActivity {
-
- //用户名,密码,再次输入的密码的控件
- private EditText et_user_name,et_psw,et_psw_again;
- //用户名,密码,再次输入的密码的控件的获取值
- private String userName,psw,pswAgain;
- private RadioGroup Sex;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //设置页面布局 ,注册界面
- setContentView(R.layout.activity_register);
- //设置此界面为竖屏
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- init();
- }
-
- private void init() {
-
- //从activity_register.xml 页面中获取对应的UI控件
- Button btn_register = (Button) findViewById(R.id.btn_register);
- et_user_name= (EditText) findViewById(R.id.et_user_name);
- et_psw= (EditText) findViewById(R.id.et_psw);
- et_psw_again= (EditText) findViewById(R.id.et_psw_again);
- Sex= (RadioGroup) findViewById(R.id.SexRadio);
- //注册按钮
- btn_register.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- //获取输入在相应控件中的字符串
- getEditString();
- //判断输入框内容
- int sex;
- int sexChoseId = Sex.getCheckedRadioButtonId();
- switch (sexChoseId) {
- case R.id.mainRegisterRdBtnFemale:
- sex = 0;
- break;
- case R.id.mainRegisterRdBtnMale:
- sex = 1;
- break;
- default:
- sex = -1;
- break;
- }
-
- if(TextUtils.isEmpty(userName)){
- Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
- }else if(TextUtils.isEmpty(psw)){
- Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
- }else if(TextUtils.isEmpty(pswAgain)) {
- Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
- } else if (sex<0){
- Toast.makeText(RegisterActivity.this, "请选择性别", Toast.LENGTH_SHORT).show();
- }else if(!psw.equals(pswAgain)){
- Toast.makeText(RegisterActivity.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
-
- /**
- *从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
- */
- }else if(isExistUserName(userName)){
- Toast.makeText(RegisterActivity.this, "此账户名已经存在", Toast.LENGTH_SHORT).show();
-
- }else{
- Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
- //把账号、密码和账号标识保存到sp里面
- /**
- * 保存账号和密码到SharedPreferences中
- */
- saveRegisterInfo(userName, psw);
- //注册成功后把账号传递到LoginActivity.java中
- // 返回值到loginActivity显示
- Intent data = new Intent();
- data.putExtra("userName", userName);
- setResult(RESULT_OK, data);
- //RESULT_OK为Activity系统常量,状态码为-1,
- // 表示此页面下的内容操作成功将data返回到上一页面,如果是用back返回过去的则不存在用setResult传递data值
- RegisterActivity.this.finish();
- }
- }
- });
- }
- /**
- * 获取控件中的字符串
- */
- private void getEditString(){
- userName=et_user_name.getText().toString().trim();
- psw=et_psw.getText().toString().trim();
- pswAgain=et_psw_again.getText().toString().trim();
- }
- /**
- * 从SharedPreferences中读取输入的用户名,判断SharedPreferences中是否有此用户名
- */
- private boolean isExistUserName(String userName){
- boolean has_userName=false;
- //mode_private SharedPreferences sp = getSharedPreferences( );
- // "loginInfo", MODE_PRIVATE
- SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
- //获取密码
- String spPsw=sp.getString(userName, "");//传入用户名获取密码
- //如果密码不为空则确实保存过这个用户名
- if(!TextUtils.isEmpty(spPsw)) {
- has_userName=true;
- }
- return has_userName;
- }
- /**
- * 保存账号和密码到SharedPreferences中SharedPreferences
- */
- private void saveRegisterInfo(String userName,String psw){
- String md5Psw = MD5Utils.md5(psw);//把密码用MD5加密
- //loginInfo表示文件名, mode_private SharedPreferences sp = getSharedPreferences( );
- SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
-
- //获取编辑器, SharedPreferences.Editor editor -> sp.edit();
- SharedPreferences.Editor editor=sp.edit();
- //以用户名为key,密码为value保存在SharedPreferences中
- //key,value,如键值对,editor.putString(用户名,密码);
- editor.putString(userName, md5Psw);
- //提交修改 editor.commit();
- editor.apply();
- }
- }
- ————————————————
- 版权声明:本文为CSDN博主「八归少年」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
- 原文链接:https://blog.csdn.net/yang_study_first/article/details/82146289
- 在注册界面上点击“立即注册”按钮后,打开注册成功界面,将注册的信息显示在界面上;(按钮点击5分,打开界面5分,显示信息20分)<?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" >
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:background="@color/colorPrimary"
- android:gravity="center_vertical">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="注册"
- android:textSize="30sp"/>
-
- </RelativeLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="20dp"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:background="@drawable/shap"
- android:orientation="vertical" >
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/et_user_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:paddingTop="2dp"
- android:paddingBottom="2dp"
- android:paddingLeft="6dp"
- android:hint="账号"
- android:singleLine="true"
- android:maxLength="15"
- android:background="#ffffff"
- android:textSize="18sp" />
- </LinearLayout>
-
-
- <LinearLayout
- android:background="@drawable/shap"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:paddingTop="2dp"
- android:paddingBottom="2dp"
- android:paddingLeft="6dp"
- android:singleLine="true"
- android:hint="请输入密码"
- android:inputType="textPassword"
- android:maxLength="15"
- android:background="#ffffff"
- android:id="@+id/et_psw"
- android:textSize="18sp" />
- </LinearLayout>
-
- <LinearLayout
- android:background="@drawable/shap"
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:paddingTop="2dp"
- android:paddingBottom="2dp"
- android:paddingLeft="6dp"
- android:singleLine="true"
- android:hint="请再次输入密码"
- android:inputType="textPassword"
- android:maxLength="15"
- android:background="#ffffff"
- android:id="@+id/et_psw_again"
- android:textSize="18sp" />
- </LinearLayout>
-
- <LinearLayout
- android:background="@drawable/shap"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="0px"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:layout_marginLeft="10dp"
- android:paddingLeft="6dp"
- android:gravity="left"
- android:text="性别"
- android:textSize="18sp" />
- <RadioGroup
- android:layout_width="0px"
- android:layout_height="wrap_content"
- android:layout_weight="2.6"
- android:id="@+id/SexRadio"
- android:paddingLeft="5dp">
- <RadioButton
- android:id="@+id/mainRegisterRdBtnFemale"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="女生"/>
- <RadioButton
- android:id="@+id/mainRegisterRdBtnMale"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="男生"/>
- </RadioGroup>
- </LinearLayout>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="50dp"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp"
- android:paddingTop="2dp"
- android:paddingBottom="2dp"
- android:paddingLeft="6dp"
- android:hint="学校"
- android:singleLine="true"
- android:maxLength="15"
- android:background="#ffffff"
- android:textSize="18sp" />
- </LinearLayout>
-
- </LinearLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="10dp"
- android:layout_marginLeft="10dp"
- android:layout_marginRight="10dp" >
- <Button
- android:id="@+id/btn_register"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:layout_marginLeft="20dp"
- android:layout_marginRight="20dp"
- android:layout_marginTop="40dp"
- android:text="注册"
- android:textSize="24sp"
- android:background="@color/colorPrimary" />
- </RelativeLayout>
-
- </LinearLayout>
- ————————————————
返回主界面进行登录,将”记住密码”复选框选中,点击登录按钮把账号和密码保存到手机中。(复选框功能10分,保存信息20分)
<?xml version="1.0" encoding="utf-8"?>
<!--登录界面,用LinearLayout-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="35dp">
<ImageView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_marginLeft="20dp"
android:id="@+id/qq"
android:layout_width="wrap_content"
android:layout_marginTop="35dp"
android:text="仿QQ"
android:textSize="24sp"
android:layout_height="wrap_content" />
</LinearLayout>
<!--输入框-->
<EditText
android:id="@+id/et_user_name"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="60dp"
android:hint="账号"
android:textSize="20sp" />
<!--输入框-->
<EditText
android:id="@+id/et_psw"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:hint="密码"
android:textSize="20sp"
android:inputType="textPassword"/>
<!--按钮-->
<Button
android:id="@+id/btn_login"
android:text="登录"
android:background="#1E90FF"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_width="320dp"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_register"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新用户注册"/>
<!--layout_weight="1" layout_width="0dp"实现均分效果-->
<TextView
android:id="@+id/tv_find_psw"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?" />
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录即代表阅读并同意服务条款" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>