此範例為使用checkBox紀錄使用者登入之帳號密碼相關資訊,
google了許多範例都閹割了一些必要功能,
例如重新開啟app後rememberMe功能就失效之類等等,
重新reWork測試後為功能較完整的一份code如下所示
Xml布局檔
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.escanui.UserInfoActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="IP:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:hint="type your IP address" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Username:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoCompleteTextView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:hint="type your user ID" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Password:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editPassword1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:hint="type your user password"
android:inputType="textPassword" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Data path:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ems="10"
android:hint="type file path" />
</LinearLayout>
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirm" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remeber me" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity.java
package com.example.remeberme;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
public class MainActivity extends Activity {
private final String PREFERENCES_NAME = "userinfo";
private EditText username,password;
private CheckBox cbRemember;
private String userName,passWord;
private Boolean isRemember = false;
/** Called when the activity is first created. */ @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
SharedPreferences preferences =
getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
username.setText(preferences.getString("UserName", null));
cbRemember.setChecked(preferences.getBoolean("Remember", true));
if(cbRemember.isChecked()){
password.setText(preferences.getString("PassWord", null));
}else{
password.setText(null);
}
}
/**
* 初始化UI控件
. */
private void initializeViews(){
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
cbRemember = (CheckBox)findViewById(R.id.ischecked);
cbRemember.setOnCheckedChangeListener
(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged
(CompoundButton buttonView, boolean isChecked) {
isRemember = isChecked;
}
});
}
@Override
public void onStop() {
super.onStop();
SharedPreferences agPreferences =
getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor =
agPreferences.edit();
userName = username.getText().toString();
passWord = password.getText().toString();
editor.putString("UserName", userName);
editor.putString("PassWord", passWord);
editor.putBoolean("Remember", isRemember);
editor.commit();
}
}
文章標籤
全站熱搜
留言列表