Voici le layourt de l'interface:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:background="@drawable/ima_home"
>
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp"
android:ems="10"
android:hint="@string/userlog"
android:imeOptions="actionNext"
android:inputType="textNoSuggestions" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="23dp"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="@string/password"
android:inputType="textPassword" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="20dp"
android:text="@string/chbSave" />
<Button
android:id="@+id/btnlogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox1"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/btnLogin"
android:layout_marginTop="40dp"/>
</RelativeLayout>
Les codes sources de l'Activité qui gere la préférence utilisateur:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity implements OnClickListener{
private String username, password;
private Button ok;
private EditText editTextUsername, editTextPassword;
private CheckBox saveLoginCheckBox;
private SharedPreferences loginPreferences;
private SharedPreferences.Editor loginPrefsEditor;
private Boolean saveLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
ok = (Button) findViewById(R.id.btnlogin);
ok.setOnClickListener((OnClickListener) this);
editTextUsername = (EditText) findViewById(R.id.editText1);
editTextPassword = (EditText) findViewById(R.id.editText2);
saveLoginCheckBox = (CheckBox) findViewById(R.id.checkBox1);
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
editTextUsername .setText(loginPreferences.getString("username", ""));
editTextPassword.setText(loginPreferences.getString("password", ""));
saveLoginCheckBox.setChecked(true);
}
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view== ok) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextUsername.getWindowToken(), 0);
username = editTextUsername.getText().toString();
password = editTextPassword.getText().toString();
if (saveLoginCheckBox.isChecked()) {
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", username);
loginPrefsEditor.putString("password", password);
loginPrefsEditor.commit();
} else {
loginPrefsEditor.clear();
loginPrefsEditor.commit();
}
doAction();
}
}
public void doAction() {
startActivity(new Intent(Login.this, ActivityDrawer.class));
Login.this.finish();
}
}