标签:
SharePreferenceService类
package com.anlen.experimentfour; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class SharePreferenceService { public static void saveInfo(Context context,String name,String password){ SharedPreferences sp=context.getSharedPreferences("config", Context.MODE_PRIVATE); Editor ed=sp.edit(); ed.putString("username", name); ed.putString("password", password); ed.commit(); } }
MainActivity类
package com.anlen.experimentfour; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { EditText username; EditText password; Button login; CheckBox check; @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); check = (CheckBox) findViewById(R.id.checkBox1); SharedPreferences sp=getSharedPreferences("config", Context.MODE_PRIVATE); username.setText(sp.getString("username", "")); password.setText(sp.getString("password", "")); } public void login(View view){ String names; String passwords; names = username.getText().toString().trim(); passwords = password.getText().toString().trim(); if(TextUtils.isEmpty(names)||TextUtils.isEmpty(passwords)){ Toast.makeText(this, "账户或者密码不能为空", Toast.LENGTH_SHORT); } if(check.isChecked()){ SharePreferenceService.saveInfo(this, names, passwords); Toast.makeText(this, "保存信息到config.xml中", Toast.LENGTH_SHORT).show(); } } }
布局:
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/namelaber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/username" /> <EditText android:inputType="text" android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/namelaber" /> <TextView android:id="@+id/passwordlaber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/username" android:text="@string/password" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/passwordlaber" android:inputType="textPassword" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_below="@+id/password" android:checked="true" android:text="@string/remember" /> <Button android:onClick="login" android:layout_width="100dp" android:layout_height="50dp" android:layout_below="@+id/password" android:layout_alignParentRight="true" android:text="@string/login" /> </RelativeLayout> </LinearLayout>
标签:
原文地址:http://my.oschina.net/anlen/blog/505282