<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:background="#E6E6E6" android:orientation="vertical" > <ImageView android:id="@+id/iv_head" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:src="@drawable/dddss" /> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/iv_head" android:layout_margin="10dp" android:background="#ffffff" android:orientation="vertical" > <RelativeLayout android:id="@+id/rl_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" > <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="账号" /> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/tv_name" android:background="@null" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#E6E6E6" /> <RelativeLayout android:id="@+id/rl_userpsw" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" > <TextView android:id="@+id/tv_psw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="密码" /> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/tv_psw" android:inputType="textPassword" android:background="@null" /> </RelativeLayout> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/layout" android:layout_centerHorizontal="true" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="20dp" android:background="#3C8DC4" android:text="登录" android:textColor="#ffffff" android:onClick="click"/> </RelativeLayout>
package com.example.xff; import java.util.HashMap; import java.util.Map; import android.support.v7.app.ActionBarActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText et1 = (EditText) findViewById(R.id.et_number); EditText et2 = (EditText) findViewById(R.id.et_password); String s1 = et1.getText().toString().trim(); String s2 = et2.getText().toString().trim(); Button b1 = (Button) findViewById(R.id.btn_login); SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString("zh", s1); editor.putString("mima", s2); // 存入String类型数据 //editor.putInt("age", 8); // 存入int类型数据 editor.commit(); } public void click(View v){ SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE); String ac= sp.getString("zh",null); String mm= sp.getString("mima",null); Map<String,String> userMap=new HashMap<String,String>(); userMap.put("zh",ac); userMap.put("mima",mm); Toast.makeText(this, "登陆成功", 0).show(); Toast.makeText(this, "保存成功", 0).show();