码迷,mamicode.com
首页 > 其他好文 > 详细

数据持久化技术

时间:2016-05-12 17:29:51      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:





技术分享


主要实现登录与记住密码功能
主要步骤如下:
   登录功能
1、login.xml
Step1:首先建立drawable文件夹, 创建logintopbg_roundcorner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#55FFFFFF"/>
    <corners android:radius="15dp"/>
</shape>
Step2:创建 btn_select.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/btn_shape" android:state_pressed="false" ></item>
    <item android:drawable="@drawable/btn_shape_after" android:state_pressed="true"></item>

</selector>
其中 btn_shape.xml 文件和 btn_shape_after.xml 文件如下
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid 
        android:color="#FF72CAE1"
        />
    <corners 
        android:radius="10dp"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#87cefa"/>
    <corners android:radius="10dp"/>
</shape>
Step3:定义 login_top.xml 文件,布局如下
<pre name="code" class="html"><?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:background="@android:drawable/edit_text"
        android:ems="10"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/cbRememberPass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnLogin"
        android:layout_alignBottom="@+id/btnLogin"
        android:layout_alignLeft="@+id/etPassword"
        android:text="记住密码"
        android:textSize="20sp" />

    <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:background="#FF72CAE1"
        android:onClick="login"
        android:text="@string/btnLogin" />

</RelativeLayout>



2、activity_login.xml 文件
<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>
3.activity_bottom.xml文件
<?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" >

    <TextView
        android:id="@+id/tvRegist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="18dp"
        android:text="@string/tvRegister"
        android:autoLink="all"
        android:textColorLink="#FF0066CC" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="24dp"
        android:src="@drawable/panda" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="28dp"
        android:src="@drawable/icon" />

</RelativeLayout>
4.loginActivity内容如下
public class LoginActivity extends Activity {
	private EditText etUsername;
	private EditText etPassword;
	private CheckBox cbIsRememberPass;
	private SharedPreferences sharedPreferences;
	private boolean rememberpassword;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		initViews();
		initValues();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (rememberpassword) {
			cbIsRememberPass.setChecked(true);
			etPassword.setOnFocusChangeListener(new OnFocusChangeListener() {
				@Override
				public void onFocusChange(View view, boolean flag) {
					if (flag) {
						String username = etUsername.getText().toString();
						String savedUsername = sharedPreferences.getString(
								"username", "");
						if (username.equals(savedUsername)){
							String password=sharedPreferences.getString(
									username, "");
							etPassword.setText(password);
							etPassword.setSelection(password.length());
						}
						
					}
				}
			});

		}
	}

	private void initValues() {
		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		rememberpassword = sharedPreferences.getBoolean("rememberpassword",
				false);
	}

	private void initViews() {
		etUsername = (EditText) findViewById(R.id.etUsername);
		etPassword = (EditText) findViewById(R.id.etPassword);
		cbIsRememberPass = (CheckBox) findViewById(R.id.cbRememberPass);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void login(View view) {
		
	}

}








数据持久化技术

标签:

原文地址:http://blog.csdn.net/acquaaintanceshipzxy/article/details/51361092

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!