用户登录,记住密码

来源:互联网 发布:卓大王yeile知乎 编辑:程序博客网 时间:2024/06/05 14:59

MainActivity

public class MainActivity extends Activity {    // 声明变量    private EditText username, password;    private CheckBox remember, free;    private Button login;    private SharedPreferences sp;    private SharedPreferences.Editor editor;    private boolean isRemember;    private boolean isfree;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        // 初始化控件        username = (EditText) findViewById(R.id.username);        password = (EditText) findViewById(R.id.password);        login = (Button) findViewById(R.id.button1);        remember = (CheckBox) findViewById(R.id.rememer);        free = (CheckBox) findViewById(R.id.free);        // 创建文件夹        sp = getSharedPreferences("mydate", MODE_PRIVATE);        // 调用修改的方法        editor = sp.edit();        login.setOnClickListener(new OnClickListener() {            public void onClick(View v) {                // 得到输入框中的值                String name = username.getText().toString().trim();                String pass = password.getText().toString().trim();                if (name == null || "".equals(name)) {                    Toast.makeText(MainActivity.this, "用户名不能为空", 0).show();                } else if (pass == null || "".equals(pass)) {                    Toast.makeText(MainActivity.this, "密码不能为空", 0).show();                } else {                    // 如果用户名和密码正确,则跳转到第二个页面                    if ("admin".equals(name) && "123".equals(pass)) {                        // 判断是否记住密码                        boolean checked = remember.isChecked();                        if (checked) {                            // 如果选中则记录输入的信息                            editor.putBoolean("isRemember", true);                            editor.putString("name", name);                            editor.putString("pass", pass);                        } else {                            // 没有选中                            editor.putBoolean("isRemember", false);                        }                        // 判断是否自动登录                        boolean checked2 = free.isChecked();                        if (checked2) {                            editor.putBoolean("isfree", true);                        } else {                            editor.putBoolean("isfree", false);                        }                        editor.commit();                        // 跳转                        Intent intent = new Intent(MainActivity.this,                                SuccessActivity.class);                        startActivity(intent);                    } else {                        Toast.makeText(MainActivity.this, "用户名或密码错误,请重新输入", 0)                                .show();                    }                }            }        });        // 如果记住密码,则给输入框赋值        isRemember = sp.getBoolean("isRemember", false);        isfree = sp.getBoolean("isfree", false);        if (isRemember) {            username.setText(sp.getString("name", ""));            password.setText(sp.getString("pass", ""));            remember.setChecked(true);        }        //如果自动登录,则让CheckBox为选中状态,直接进行跳转        if (isfree) {            free.setChecked(true);            startActivity(new Intent(MainActivity.this, SuccessActivity.class));        }    }}

SuccessActivity

public class SuccessActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_success);    }}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <TextView        android:id="@+id/text1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="17dp"        android:layout_marginTop="35dp"        android:text="用户名" />    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/text1"        android:layout_below="@+id/text1"        android:layout_marginTop="22dp"        android:text="密码" />    <EditText        android:id="@+id/password"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBaseline="@+id/textView1"        android:layout_alignBottom="@+id/textView1"        android:layout_alignLeft="@+id/username"        android:ems="10"        android:inputType="textPassword" >        <requestFocus />    </EditText>    <EditText        android:id="@+id/username"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_above="@+id/textView1"        android:layout_marginLeft="19dp"        android:layout_toRightOf="@+id/text1"        android:ems="10" />    <CheckBox        android:id="@+id/free"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/rememer"        android:layout_below="@+id/rememer"        android:layout_marginTop="20dp"        android:text="自动登录" />    <CheckBox        android:id="@+id/rememer"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/password"        android:layout_below="@+id/password"        android:layout_marginTop="52dp"        android:text="记住密码" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/free"        android:layout_below="@+id/free"        android:layout_marginTop="34dp"        android:onClick="test"        android:text="登录" /></RelativeLayout>

activity_success.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".SuccessActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="@string/hello_world" /></RelativeLayout>
原创粉丝点击