Preferences Framework
Android provides a standardized framework for setting preferences across all applications. The framework uses category preferences and screens to group related settings. PreferenceCategory is used to declare a set of preferences into one category. PreferenceScreen presents a group of preferences in a new screen.Android Login Example Code
LoginActivity.java Code
package
android.code.login;
import
android.app.Activity;
import
android.content.Intent;
import
android.content.SharedPreferences;
import
android.os.Bundle;
import
android.preference.PreferenceManager;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
public class LoginActivity extends Activity {
      SharedPreferences myprefs;    
      EditText userET, passwordET;
      Button loginBT;   
      static String username;
    @Override
    public void onCreate(Bundle
savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        myprefs = PreferenceManager.getDefaultSharedPreferences(this);
            username = myprefs.getString("username", null);
            final String password
= myprefs.getString("password", null);
            if (username != null &&
password != null){
                  setContentView(R.layout.main);
                  userET =
(EditText)findViewById(R.id.editTextUser);
                  passwordET =
(EditText)findViewById(R.id.editPassword);
                  loginBT =
(Button)findViewById(R.id.btnLogin);
                  loginBT.setOnClickListener(new
OnClickListener() {
                        public void onClick(View v)
{
                              try {
                                    if(username.equals(userET.getText().toString())
                                                &&
password.equals(
                                                            passwordET.getText().toString()))
{
                                          Toast.makeText(LoginActivity.this,
                                                      "login
passed!!",
                                                      Toast.LENGTH_SHORT).show();   
                                          //user =
userET.getText();
                                          startActivity(new Intent("android.code.login.activity2"));
                                    } else {
                                          Toast.makeText(LoginActivity.this,
                                                      "login
failed!!",
                                                      Toast.LENGTH_SHORT).show();
                                    }
                              } catch (Exception e) {
                                    e.printStackTrace();
                              }
                        }
                  });
            } else {
                  Intent i = new Intent(this, myPreferences.class);
                  startActivity(i);
            }
            /*
            Button bn = (Button)findViewById(R.id.button2);
            bn.setOnClickListener(new
OnClickListener() {
                  public void onClick(View v) {
                        //startActivity(new
Intent("com.app.activity2")); // Next page
                  } // onClick(View v)
            }); // bn.setOnClickListener 
            */
    }
}
myPreferences.java Code
package
android.code.login;
import
android.os.Bundle;
import
android.preference.PreferenceActivity;
public class myPreferences extends
PreferenceActivity {
      //public static final String
KEY_MY_PREFERENCE = "my_preference";
    //public static final String
KEY_ADVANCED_CHECKBOX_PREFERENCE = "advanced_checkbox_preference";
    public static final String USER_NAME = "username";
    public static final String PASSWORD = "password";
      public void onCreate(Bundle
savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferrences);
      }     
}
main.xml Code
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#4e0b02"
    android:orientation="vertical"
>
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
>
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Android
Login"
                android:textColor="#585ff6"
                android:textStyle="bold"
                android:textSize="30dp"
/>
            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="fill_parent"
                android:layout_height="66dp"
                android:layout_gravity="center_vertical"
                android:layout_marginTop="10dp"
                android:src="@drawable/ic_launcher"
/>
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge"
/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Username"
                android:textAppearance="?android:attr/textAppearanceMedium"
/>
            <EditText
                android:id="@+id/editTextUser"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
>
                <requestFocus />
            </EditText>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Password"
                android:textAppearance="?android:attr/textAppearanceMedium"
/>
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
>
                <EditText
                    android:id="@+id/editPassword"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="7.61"
                    android:inputType="textPassword"
/>
                <Button
                    android:id="@+id/button2"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Clear"
/>
                <Button
                    android:id="@+id/btnLogin"
                    android:layout_width="80dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_weight="1"
                    android:text="Log In"
/>
            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" "
                android:textAppearance="?android:attr/textAppearanceLarge"
/>
            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:text="Version 0.1
"
                android:textColor="#FF00FF"
                android:textSize="20dp"
/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>
perferences.xml Code
<?xml version="1.0"
encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
      <EditTextPreference 
          android:title="User Name"
            android:key="username"
            android:summary="Please provide
Your User Name">
      </EditTextPreference>
      <EditTextPreference 
          android:title="Password"        
            android:key="password"
            android:summary="Please enter
your password">
      </EditTextPreference>
</PreferenceScreen>
AndroidManifest.xml Code
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.code.login"
    android:versionCode="1"
    android:versionName="1.0"
>
    <uses-sdk android:minSdkVersion="10"
/>
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
>
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
>
            <intent-filter>
                <action android:name="android.intent.action.MAIN"
/>
                <category android:name="android.intent.category.LAUNCHER"
/>
            </intent-filter>
        </activity>
        <activity android:name=".myPreferences"
          android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.code.login.myPreferences"/>
                <category android:name="android.intent.category.DEFAULT"
/>
            </intent-filter>            
        </activity>
        <activity
            android:name="android.code.login.activity2"
            android:label="@string/app_name"
>
            <intent-filter>
                <action android:name="android.code.login.activity2"
/>
                <category android:name="android.intent.category.DEFAULT"
/>
            </intent-filter>
        </activity>
    </application>
</manifest>
Download Android Login Example Code
More info
Share Preference
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android  สอนเขียนโปรแกรม Android
http://androidcontrol.blogspot.com/2012/01/beginning-android-training-android.html
ติดต่อ amphancm@gmail.com
ติดต่อ amphancm@gmail.com



