1 登录界面。 2 记住密码设置 3 自动登录。 package cn.edu.bzu.r2; import java.util.Timer; import java.util.TimerTask; import com.r2.lgoin.R; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText userName; private EditText password; private CheckBox checkBox; private CheckBox chechBox1; SharedPreferences sharedPreferences; Editor editor; Timer timer; Intent intent ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userName=(EditText) findViewById(R.id.setusername); password=(EditText) findViewById(R.id.setpass); checkBox=(CheckBox) findViewById(R.id.CBRememberPassword); chechBox1=(CheckBox) findViewById(R.id.CBAutmaticLanging); //定时跳转Activity timer=new Timer(); //新建文档 sharedPreferences=getSharedPreferences("loginData", MODE_PRIVATE); boolean b=false,d=false; //向新建的文档总读取数据 b=sharedPreferences.getBoolean("cheackBox", false); d=sharedPreferences.getBoolean("cheackBox1", false); if(b==true&&d==true){ userName.setText(sharedPreferences.getString("uname", "")); password.setText(sharedPreferences.getString("upassword", "")); checkBox.setChecked(true); chechBox1.setChecked(true); tz(); }else if(b==true&&d==false){ userName.setText(sharedPreferences.getString("uname", "")); password.setText(sharedPreferences.getString("upassword", "")); checkBox.setChecked(true); chechBox1.setChecked(false); } } //button的点击事件 public void dj(View view){ String name=userName.getText().toString(); String pass=password.getText().toString(); if(!(TextUtils.isEmpty(name))&&!(TextUtils.isEmpty(pass))){ if("zhangsan".equals(name)&&"123".equals(pass)){ editor=sharedPreferences.edit(); if(checkBox.isChecked()&&chechBox1.isChecked()){ //向新建的文档中写入数据 editor.putString("uname", "zhangsan"); editor.putString("upassword", "123"); editor.putBoolean("cheackBox", true); editor.putBoolean("cheackBox1", true); }else if(checkBox.isChecked()&&!(chechBox1.isChecked())){ editor.putString("uname", "zhangsan"); editor.putString("upassword", "123"); editor.putBoolean("cheackBox", true); editor.putBoolean("cheackBox1", false); } else{ //清楚新建文档中的数据 editor.clear(); } //执行对新建文档的操作 editor.commit(); tz(); }else{ Toast.makeText(this, "用户名或密码错误。。", Toast.LENGTH_LONG).show(); } } } //Activity跳转 public void tz(){ intent =new Intent(); intent.setClass(this, ChengGong.class); Toast.makeText(this, "正在登陆。。。", Toast.LENGTH_SHORT).show(); TimerTask tast=new TimerTask() { @Override public void run(){ startActivity(intent); finish(); } }; timer.schedule(tast,1000); } } package cn.edu.bzu.r2; import com.r2.lgoin.R; import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class ChengGong extends Activity { private TextView tv1; SharedPreferences sharedPreferences; Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cheng_gong); tv1=(TextView) findViewById(R.id.tv1); sharedPreferences=getSharedPreferences("loginData", MODE_PRIVATE); boolean b=sharedPreferences.getBoolean("cheackBox1", false); if(b==true){ tv1.setText("自动登录成功"); }else{tv1.setText("登录成功");} } public void qx(View view){ editor=sharedPreferences.edit(); editor.putBoolean("cheackBox1", false); editor.commit(); Toast.makeText(this, "您取消了自动登录", Toast.LENGTH_LONG).show(); } } <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/jianbianse" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="cn.edu.bzu.r2.MainActivity" > <RelativeLayout android:id="@+id/rl1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="15dip" android:layout_margin="15dip" android:background="@drawable/loginshang" > <TextView android:id="@+id/showusername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="5dp" android:text="@string/showusername" style="@style/showusername"/> <EditText android:id="@+id/setusername" android:layout_width="fill_parent" android:layout_height="40dp" android:hint="@string/userhint" android:layout_below="@id/showusername" android:singleLine="true" android:inputType="text" android:background="@drawable/edyuanjiao" /> <TextView android:id="@+id/showpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/setusername" android:layout_marginTop="5dp" android:text="@string/showpass" style="@style/showusername"/> <EditText android:id="@+id/setpass" android:layout_width="fill_parent" android:layout_height="40dp" android:hint="@string/passhint" android:layout_below="@id/showpass" android:singleLine="true" android:inputType="textPassword" android:background="@drawable/edyuanjiao" /> <Button android:id="@+id/login" android:layout_width="100dp" android:layout_height="wrap_content" android:text="@string/login" android:background="@drawable/btn_login" android:layout_below="@+id/CBAutmaticLanging" android:padding="5dp" android:layout_margin="10dp" android:onClick="dj" android:layout_alignRight="@+id/setpass" style="@style/btn_text" /> <CheckBox android:id="@+id/CBRememberPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/CBRememberPassword" android:layout_below="@+id/setpass" android:layout_toLeftOf="@+id/login" android:textColor="@android:color/holo_red_dark" /> <CheckBox android:id="@+id/CBAutmaticLanging" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/CBAutmaticLanging" android:layout_below="@+id/setpass" android:layout_toRightOf="@+id/CBRememberPassword" android:textColor="@android:color/holo_red_dark" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl2" android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="@string/zhuce" /> <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/tuzi" android:layout_alignParentBottom="true" android:layout_alignParentRight="true"/> <ImageView android:layout_width="128dp" android:layout_height="128dp" android:src="@drawable/font" android:layout_alignTop="@+id/iv1" android:layout_toLeftOf="@+id/iv1" android:layout_marginRight="20dp"/> </RelativeLayout> </LinearLayout> <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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".ChengGong" > <Button android:id="@+id/qx" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/tv1" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:onClick="qx" android:text="@string/qx" /> <TextView android:id="@+id/tv1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="104dp" android:text="@string/cg" /> </RelativeLayout>