Android 简单登录注册
Posted yangchas
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 简单登录注册相关的知识,希望对你有一定的参考价值。
下载链接 (ps:只包含main下主要文件)
目录结构如下:
java代码>> LoginActivity.java MainActivity.java RegistActivity.java layout布局>> activity_login.xml activity_main.xml activity_regist.xml
LoginActivity.java
package com.example.user.registlogintest; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class LoginActivity extends AppCompatActivity private SharedPreferences pref; private SharedPreferences.Editor editor; private CheckBox rememberpass; private EditText nameEt; private EditText pwdEt; private Button login_btn; private Button regist_btn; @Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); pref = getSharedPreferences("data", 0); rememberpass = (CheckBox) findViewById(R.id.remember); nameEt = (EditText) findViewById(R.id.account); pwdEt = (EditText) findViewById(R.id.pwd); login_btn = (Button) findViewById(R.id.login); regist_btn = (Button) findViewById(R.id.regist); boolean isRemember = pref.getBoolean("remember_pass", false); if (isRemember) //取不到的话返回空串 String account = pref.getString("account", ""); String password = pref.getString("password", ""); //二次登录的时候,第一次的登录信息已经显示。 nameEt.setText(account); pwdEt.setText(password); rememberpass.setChecked(true); regist_btn.setOnClickListener(new View.OnClickListener() @Override public void onClick(View v) Intent intent = new Intent(LoginActivity.this, RegistActivity.class); startActivity(intent); finish(); ); login_btn.setOnClickListener(new View.OnClickListener() @Override public void onClick(View v) String name = nameEt.getText().toString(); String pwd = pwdEt.getText().toString(); SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE); String nameSP = pref.getString("name", ""); String password = pref.getString("password", ""); editor = pref.edit(); if (name.equals(nameSP) && pwd.equals(password)) if (rememberpass.isChecked()) //选中了复选框 //先存数据到SP editor.putString("account", name); editor.putString("password", pwd); editor.putBoolean("remember_pass", true); else //清空editor editor.putBoolean("remember_pass", false); //提交数据 editor.commit(); //跳到主活动 Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); else Toast.makeText(LoginActivity.this, "您的登录名或密码有误!!", Toast.LENGTH_SHORT).show(); );
MainActivity.java
package com.example.user.registlogintest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; public class MainActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
RegistActivity.java
package com.example.user.registlogintest; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; public class RegistActivity extends AppCompatActivity implements View.OnClickListener private EditText name; private EditText password; private RadioButton sex1; private RadioButton sex2; private CheckBox hobby1; private CheckBox hobby2; private CheckBox hobby3; @Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_regist); name = (EditText) findViewById(R.id.ed_name); password = (EditText) findViewById(R.id.et_pwd); sex1 = (RadioButton) findViewById(R.id.ed_sex1); sex2 = (RadioButton) findViewById(R.id.ed_sex2); hobby1 = (CheckBox) findViewById(R.id.ed_hobby1); hobby2 = (CheckBox) findViewById(R.id.ed_hobby2); hobby3 = (CheckBox) findViewById(R.id.ed_hobby2); Button button = (Button) findViewById(R.id.btn_submit); button.setOnClickListener(this); @Override public void onClick(View v) switch (v.getId()) case R.id.btn_submit: String result = ""; String nameStr = name.getText().toString(); String pwdStr = password.getText().toString(); String radiostr1 = sex1.isChecked() ? "男" : ""; String radioStr2 = sex2.isChecked() ? "女" : ""; String hobby1Str = hobby1.getText().toString(); String hobby2Str = hobby2.getText().toString(); String hobby3Str = hobby3.getText().toString(); if (!nameStr.isEmpty()) result += nameStr; if (!pwdStr.isEmpty()) result += pwdStr; if (!radioStr1.isEmpty()) result += radioStr1; if (!radioStr2.isEmpty()) result += radioStr2; if (!hobby1Str.isEmpty()) result += hobby1Str; if (!hobby2Str.isEmpty()) result += hobby2Str; if (!hobby3Str.isEmpty()) result += hobby3Str; Toast.makeText(RegistActivity.this, result, Toast.LENGTH_LONG).show(); SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit(); editor.putString("name", nameStr); editor.putString("password", pwdStr); editor.putString("sex", sex1.isChecked() ? "男" : "女"); editor.apply(); Toast.makeText(this, "注册成功!!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); finish(); break;
activity_login.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_login" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" android:orientation="vertical" tools:context="com.example.user.registlogintest.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:text="登录名:" android:layout_gravity="center_vertical" android:textSize="18sp" /> <EditText android:id="@+id/account" android:layout_width="0dp" android:layout_weight="1" android:layout_gravity="center_vertical" android:hint="在此处输入用户姓名" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:text="密 码:" android:layout_gravity="center_vertical" android:textSize="18sp" /> <EditText android:id="@+id/pwd" android:layout_width="0dp" android:layout_weight="1" android:layout_gravity="center_vertical" android:hint="在此处输入用户密码" android:inputType="textPassword" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="match_parent" /> <TextView android:textSize="18sp" android:text="记住登录账号和密码" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000" /> <LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="wrap_content"> <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" /> <Button android:id="@+id/regist" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="注册" /> </LinearLayout> </LinearLayout>
activity_regist.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_regist" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.example.user.registlogintest.MainActivity"> <TextView android:id="@+id/tv_message" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:padding="10dp" android:text="用户注册页面" android:textSize="20dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="姓名" /> <EditText android:id="@+id/ed_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:background="@color/background_color" android:hint="请输入姓名" android:inputType="textPersonName" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <TextView android:id="@+id/tv_pwd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="密码" /> <EditText android:id="@+id/et_pwd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:background="@color/background_color" android:hint="请输入密码" android:inputType="textPassword" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp"> <TextView android:id="@+id/tv_sex" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="性别" /> <RadioGroup android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="3" android:orientation="horizontal"> <RadioButton android:id="@+id/ed_sex1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> <RadioButton android:id="@+id/ed_sex2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </RadioGroup> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="5dp"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="爱好" /> <CheckBox android:id="@+id/ed_hobby1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="吃饭" /> <CheckBox android:id="@+id/ed_hobby2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="吃饭" /> <CheckBox android:id="@+id/ed_hobby3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="吃饭" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_submit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="提交" /> <Button android:id="@+id/btn_cancel" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout>
完!
以上是关于Android 简单登录注册的主要内容,如果未能解决你的问题,请参考以下文章