記住密碼和自動登入介面的實現(五),記住密碼登入介面

來源:互聯網
上載者:User

記住密碼和自動登入介面的實現(五),記住密碼登入介面

你不能左右天氣,但可以改變心情。你不能改變容貌,但可以掌握自己。你不能預見明天,但可以珍惜今天。


本講內容:記住密碼和自動登入介面的實現


樣本

    


下面是res/layout/activity_login.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="match_parent"    android:background="@drawable/login" >    <ImageButton        android:id="@+id/img_btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:background="@drawable/quit" />    <TextView        android:id="@+id/tv_zh"        android:layout_width="wrap_content"        android:layout_height="35dp"        android:layout_marginLeft="15dp"        android:layout_marginTop="12dp"        android:gravity="bottom"        android:text="帳號:"        android:textColor="#000000"        android:textSize="20sp" />    <EditText        android:id="@+id/et_zh"        android:layout_width="match_parent"        android:layout_height="40dp"        android:layout_below="@+id/tv_zh"        android:layout_marginLeft="15dp"        android:layout_marginRight="12dp" />    <TextView        android:id="@+id/tv_pws"        android:layout_width="wrap_content"        android:layout_height="35dp"        android:layout_below="@+id/et_zh"        android:layout_marginLeft="15dp"        android:layout_marginTop="12dp"        android:gravity="bottom"        android:text="密碼:"        android:textColor="#000000"        android:textSize="20sp" />    <EditText        android:id="@+id/et_pws"        android:layout_width="match_parent"        android:layout_height="40dp"        android:layout_below="@+id/tv_pws"        android:layout_marginLeft="15dp"        android:layout_marginRight="12dp" />    <CheckBox        android:id="@+id/cb_pws"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/et_pws"        android:layout_marginLeft="15dp"        android:text="記住密碼"        android:textColor="#000000" />    <CheckBox        android:id="@+id/cb_auto"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/cb_pws"        android:layout_marginLeft="15dp"        android:text="自動登入"        android:textColor="#000000" />    <Button        android:id="@+id/btn_login"        android:layout_width="80dp"        android:layout_height="40dp"        android:layout_alignParentRight="true"        android:layout_alignTop="@+id/cb_auto"        android:layout_below="@+id/et_pws"        android:layout_marginRight="10dp"        android:gravity="center"        android:text="登入"        android:textColor="#000000"        android:textSize="20sp" /></RelativeLayout>

下面是res/layout/activity_logo.xml 布局檔案:(登入緩衝介面)

<?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:background="@drawable/login"    android:orientation="vertical" >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="3" >        <ProgressBar            android:id="@+id/pgBar"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_centerInParent="true" />        <TextView            android:id="@+id/tv1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_below="@+id/pgBar"            android:layout_centerHorizontal="true"            android:text="正在登入..."            android:textColor="#000000"            android:textSize="20sp" />    </RelativeLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center" >        <Button            android:id="@+id/btn_back"            android:layout_width="70dp"            android:layout_height="35dp"            android:text="取消"            android:textColor="#000000"            android:textSize="15sp" />    </LinearLayout></LinearLayout>

下面是res/layout/activity_welcome.xml 布局檔案:(歡迎介面)

<?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:background="@drawable/bg"    android:orientation="vertical" >    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:gravity="center"        android:text="登陸成功,進入使用者介面"        android:textColor="#ff00ff"        android:textSize="25sp" /></LinearLayout>

下面是LoginActivity.java介面檔案:(要註冊)

public class LoginActivity extends Activity{private EditText userName,password;private CheckBox rem_pw,auto_login;private Button btn_login;private ImageButton btnQuit;private SharedPreferences sp;private String userNameValue,passwordValue;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除標題setContentView(R.layout.activity_login);initView();initData();}private void initView() {//獲得執行個體對象userName=(EditText) findViewById(R.id.et_zh);password=(EditText) findViewById(R.id.et_pws);rem_pw=(CheckBox) findViewById(R.id.cb_pws);auto_login=(CheckBox) findViewById(R.id.cb_auto);btn_login=(Button) findViewById(R.id.btn_login);btnQuit=(ImageButton) findViewById(R.id.img_btn);sp=this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);}private void initData() {//判斷記住密碼多選框的狀態if(sp.getBoolean("ISCHECK", false)){//設定預設是記錄密碼狀態rem_pw.setChecked(true);userName.setText(sp.getString("USER_NAME", ""));password.setText(sp.getString("PASSWORD", ""));//判斷自動登陸多選框狀態if(sp.getBoolean("AUTO_ISCHECH", false)){ //設定預設是自動登入狀態auto_login.setChecked(true); //跳轉介面Intent intent=new Intent(LoginActivity.this,LogoActivity.class);LoginActivity.this.startActivity(intent);}}// 登入監聽事件  現在預設為使用者名稱為:jin 密碼:1230btn_login.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {userNameValue=userName.getText().toString();passwordValue=password.getText().toString();if(userNameValue.equals("jin")&&passwordValue.equals("1230")){//Toast.makeText(LoginActivity.this,"登入成功", Toast.LENGTH_SHORT).show();//登入成功和記住密碼框為選中狀態才儲存使用者資訊if(rem_pw.isChecked()){ //記住使用者名稱、密碼Editor editor=sp.edit();editor.putString("USER_NAME", userNameValue);editor.putString("PASSWORD", passwordValue);editor.commit();}//跳轉介面Intent intent = new Intent(LoginActivity.this,LogoActivity.class);LoginActivity.this.startActivity(intent);//finish();}else{Toast.makeText(LoginActivity.this,"使用者名稱或密碼錯誤,請重新登入", Toast.LENGTH_LONG).show();}}});//監聽記住密碼多選框按鈕事件rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if(rem_pw.isChecked()){Toast.makeText(LoginActivity.this,"記住密碼已選中", Toast.LENGTH_LONG).show();sp.edit().putBoolean("ISCHECK", true).commit();}else{Toast.makeText(LoginActivity.this,"記住密碼沒有選中", Toast.LENGTH_LONG).show();sp.edit().putBoolean("ISCHECK", false).commit();}}});//監聽自動登入多選框事件auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton arg0, boolean arg1) {if(auto_login.isChecked()){Toast.makeText(LoginActivity.this,"自動登入已選中", Toast.LENGTH_LONG).show();sp.edit().putBoolean("AUTO_ISCHECK", true).commit();}else{Toast.makeText(LoginActivity.this,"自動登入沒有選中", Toast.LENGTH_LONG).show();sp.edit().putBoolean("AUTO_ISCHECK", false).commit();}}});//刪除事件(退出程式)btnQuit.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {finish();}});}}

下面是LogoActivity.java介面檔案:(要註冊)

public class LogoActivity extends Activity implements Runnable{private ProgressBar progressBar;private Button backButton;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);this.requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_logo);progressBar=(ProgressBar) findViewById(R.id.pgBar);backButton=(Button) findViewById(R.id.btn_back);progressBar.setMax(3000);//啟動一個延遲線程          new Thread(this).start();  backButton.setOnClickListener(new OnClickListener() {public void onClick(View v) {finish();}});}public void run() {try { Thread.sleep(2000);//延遲兩秒時間   startActivity(new Intent(this,WelcomeActivity.class));} catch (Exception e) {e.printStackTrace();}}}

下面是WelcomeActivity.java介面檔案:

public class WelcomeActivity extends Activity{protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_welcome);}}


Take your time and enjoy it 要原碼的、路過的、學習過的請留個言,頂個唄~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.