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"
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="com.example.demo1.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="用户名:" />
<EditText
android:id="@+id/editusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/editusername"
android:layout_marginTop="32dp"
android:text="密码:" />
<EditText
android:id="@+id/editpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/editusername"
android:ems="10" />
<CheckBox
android:id="@+id/savename"
android:layout_width="wrap_content"
android:checked="false"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/editpassword"
android:layout_marginTop="37dp"
android:text="保存用户名" />
<Button
android:id="@+id/btnlogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/savename"
android:layout_below="@+id/savename"
android:layout_marginTop="25dp"
android:text="登录" />
<Button
android:id="@+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnlogin"
android:layout_alignBottom="@+id/btnlogin"
android:layout_toRightOf="@+id/savename"
android:text="取消" />
</RelativeLayout>
java文件:
package com.example.demo1;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText edusernam;
EditText password;
CheckBox checkBox;
Button Login;
Button Cancel;
SharedPreferences pref;
Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
// SharedPreferences preferences=getSharedPreferences("mypref", MODE_PRIVATE);
// Editor editor=preferences.edit();
// editor.putString("name","张三");
// editor.putInt("age",30);
// editor.putLong("time",System.currentTimeMillis());
// editor.putBoolean("default",true);
// editor.commit();
// editor.remove("default");
// editor.commit();
// System.out.println(preferences.getString("name",""));
// System.out.println(preferences.getInt("age",0));
edusernam=(EditText) findViewById(R.id.editusername);
password=(EditText) findViewById(R.id.editpassword);
Login=(Button) findViewById(R.id.btnlogin);
Cancel=(Button) findViewById(R.id.btncancel);
checkBox=(CheckBox) findViewById(R.id.savename);
pref=getSharedPreferences("User",MODE_PRIVATE);
editor=pref.edit();
String name=pref.getString("username","");
if(name.equals("")){
checkBox.setChecked(false);
}
else {
checkBox.setChecked(true);
edusernam.setText(name);
}
Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name=edusernam.getText().toString().trim();
String password1=password.getText().toString();
if("admin".equals(name)&&"123".equals(password1)){
if(checkBox.isChecked()){
editor.putString("username",name);
editor.commit();
}
else {
editor.remove("username");
editor.commit();
}
Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this,"登陆失败",Toast.LENGTH_LONG).show();
}
}
});
}
}