編輯:關於android開發
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="logins" android:textSize="15sp" /> <EditText android:id="@+id/ed_login_name" android:layout_width="match_parent" android:hint="輸入用戶名" android:singleLine="true" android:layout_height="wrap_content" android:gravity="center" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="passd" android:textSize="15sp" /> <EditText android:id="@+id/ed_login_password" android:layout_width="match_parent" android:hint="輸入密碼" android:singleLine="true" android:layout_height="wrap_content" android:gravity="center" android:inputType="textPassword" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" > <CheckBox android:id="@+id/cb_login_dian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="點擊保存密碼" android:textSize="12sp" /> <Button android:id="@+id/bb_login_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:text="登陸" android:textSize="13sp" /> </RelativeLayout> </LinearLayout>
package org.xml.demo.viewpager;
import ogg.huanxin.huadong.R;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
/*
* 使用SharedPreferences保存數據,其背後是用xml文件存放數據,文件存放在/data/data/<package name>/shared_prefs目錄下:
*/
public class Login extends Activity {
/** 定義登錄姓名的編輯框 */
private EditText ed_Name;
/** 定義輸入密碼的編輯框 */
private EditText ed_PassWord;
/** 定義登錄的按鈕 當點擊時跳轉到其他的界面 */
private Button loginButton;
/** 定義個選擇框點擊時會記住密碼 */
private CheckBox cb_dian;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.setContentView(R.layout.login);
ed_Name = (EditText) super.findViewById(R.id.ed_login_name);
ed_PassWord = (EditText) super.findViewById(R.id.ed_login_password);
loginButton = (Button) super.findViewById(R.id.bb_login_login);
cb_dian = (CheckBox) super.findViewById(R.id.cb_login_dian);
loginButton.setOnClickListener(new MyOnClickItem());
/** 定義個方法當下次進來的時候會讀取getSharedPreferences若有則將姓名和密碼返回到界面上 */
readLogin();
}
private class MyOnClickItem implements View.OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (cb_dian.isChecked()) {
BaoCuenLogin();
Toast.makeText(Login.this, "登陸成功", Toast.LENGTH_SHORT).show();
}
Intent it = new Intent(Login.this, DialogText.class);
startActivity(it);
}
}
/** 定義個方法當下次進來的時候會讀取getSharedPreferences若有則將姓名和密碼返回到界面上 */
private void readLogin() {
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
this.ed_Name.setText(sp.getString("name", ""));
this.ed_PassWord.setText(sp.getString("password", ""));
}
/** 實現文件的保存 */
private void BaoCuenLogin() {
String name = ed_Name.getText().toString();
String passWord = ed_PassWord.getText().toString();
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
SharedPreferences.Editor ed = sp.edit();
ed.putString("name", name);
ed.putString("password", passWord);
ed.commit();// 提交
}
}
然後導出shared_prefs文件夾下的info.xml文件
android.content.SharedPreferences是一個接口,用來獲取和修改持久化存儲的數據。有三種獲取系統中保存的持久化數據的方式:
1). public SharedPreferences getPreferences (int mode)
通過Activity對象獲取,獲取的是本Activity私有的Preference,保存在系統中的xml形式的文件的名稱為這個Activity的名字,因此一個Activity只能有一個,屬於這個Activity。
2). public SharedPreferences getSharedPreferences (String name, int mode)
因為Activity繼承了ContextWrapper,因此也是通過Activity對象獲取,但是屬於整個應用程序,可以有多個,以第一參數的name為文件名保存在系統中。
3). public static SharedPreferences getDefaultSharedPreferences (Context context)
PreferenceManager的靜態函數,保存PreferenceActivity中的設置,屬於整個應用程序,但是只有一個,Android會根據包名和PreferenceActivity的布局文件來起一個名字保存。
通過以上方式取得SharedPreferences後就可以對數據進行讀取或者保存了。
Android 購物車功能的實現,android購物車實現
Android 購物車功能的實現,android購物車實現首先,眾所周知,ListView是Android最常用的控件,可以說是最簡單的控件,也可以說是最復雜的控件。 作
Android OkHttp詳解,androidokhttp詳解
Android OkHttp詳解,androidokhttp詳解來源 http://frodoking.github.io/2015/03/12/android
《Android源碼設計模式解析》讀書筆記——Android中你應該知道的設計模式
《Android源碼設計模式解析》讀書筆記——Android中你應該知道的設計模式 斷斷續續的,《Android源碼設計模式解析》也看了一遍,書中提到了很多的設計模式,
android 簡單地設置Activity界面的跳轉動畫
android 簡單地設置Activity界面的跳轉動畫 動畫這一知識點算是水比較深了,主要在自定義動畫中可是大有文章,並且技術都會了後就需要看設計能力了。 當然這些不是