标签:
今天小编学习了MiniTwitter记住密码的小案例:
界面如下:
在设计时需要注意2点:
1.只有在用户选择了记住密码并且成功登录后,才执行记住密码的操作
2.记住的不仅仅是用户名以及密码,还要记住复选框的选中状态
布局主要代码如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/btnbg_roundcorner" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <TextView android:id="@+id/tvUsername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/tvName" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/etUsername" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvUsername" android:layout_below="@+id/tvUsername" android:background="@android:drawable/edit_text" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/tvPassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etUsername" android:layout_below="@+id/etUsername" android:text="@string/tvPassword" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tvPassword" android:layout_below="@+id/tvPassword" android:layout_marginTop="16dp" android:background="@android:drawable/edit_text" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/etPassword" android:layout_below="@+id/etPassword" android:layout_marginTop="20dp" android:background="#FF72CAE1" android:text="@string/btnLogin" /> <CheckBox android:id="@+id/rememberpassword" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etPassword" android:layout_alignTop="@+id/btnLogin" android:layout_marginLeft="19dp" android:checked="true" android:text="@string/remember" /> </RelativeLayout>
<pre name="code" class="html"><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:orientation="vertical" android:background="@drawable/loginbg" 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=".LoginActivity" > <include layout="@layout/login_top"/> <include layout="@layout/login_bottom"/>" </LinearLayout>
实现登录及保存密码的主要代码如下:
import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.text.Editable; import android.text.TextUtils; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class LoginActivity extends Activity { private Button btnLogin; private EditText etUsername; private EditText etPassword; private CheckBox rememberpassword; // 声明一个SharedPreferences用于保存数据 private SharedPreferences setting = null; private static final String PREFS_NAME = "NamePwd"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); etUsername=(EditText)findViewById(R.id.etUsername); etPassword=(EditText)findViewById(R.id.etPassword); rememberpassword=(CheckBox)findViewById(R.id.rememberpassword); btnLogin=(Button)findViewById(R.id.btnLogin); setListener() ; getData(); } private void setListener() { // 为登录按钮绑定事件 btnLogin.setOnClickListener(new OnClickListener() { String username=etUsername.getText().toString(); String password=etPassword.getText().toString(); @Override public void onClick(View arg0) { if(!(TextUtils.isEmpty(username))&&(TextUtils.isEmpty(password))) { // 判断用户名和密码 if ("123".equals(username)&&"123".equals(password)) { // 跳转到首页 Intent intent = new Intent(LoginActivity.this, Login2Activity.class); startActivity(intent); finish(); } } else { // 显示错误提示 Toast.makeText(getApplicationContext(), "用户名或密码错误", Toast.LENGTH_SHORT).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } protected void onResume() { // 在界面显示数据之前得到之前存储的数据 super.onResume(); getData(); } /** * 获取存储是数据 */ private void getData() { // 得到sharedpreferences对象 setting = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); // 判断是否之前存储过用户名密码 if (setting.getBoolean("isKeep", false)) { // 如果之前存储过,则显示在相应文本框内 etUsername.setText(setting.getString("username", "")); etPassword.setText(setting.getString("password", "")); } else { // 否则显示空 etUsername.setText(""); etPassword.setText(""); } } }
标签:
原文地址:http://www.cnblogs.com/toast/p/4651034.html