Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android記住密碼後自動登錄

Android記住密碼後自動登錄

編輯:關於Android編程

 

/**
*
* @author alex
* @version 2014-7-31下午5:25:45
*
*/
public class LoginActivity extends Activity {
private EditText name;
private EditText pass;
private CheckBox isRemenber;
private CheckBox isLoginSelf;
private Button longin;
private ProgressDialog mDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name=(EditText)findViewById(R.id.et_name);
pass=(EditText)findViewById(R.id.et_pass);
isRemenber=(CheckBox)findViewById(R.id.isremenber);
isLoginSelf=(CheckBox)findViewById(R.id.isloginself);
longin=(Button)findViewById(R.id.longin);

final SharedPreferences pre=getSharedPreferences(longinvalue, MODE_WORLD_WRITEABLE);
if(pre!=null){
//記住了密碼
if(pre.getBoolean(isrmb, false)==true){
name.setText(pre.getString(name, null));
pass.setText(pre.getString(pass, null));
isRemenber.setChecked(true) ;
}
if(pre.getBoolean(islgs, false)==true){
isLoginSelf.setChecked(true);
creatDialog();
new Thread(){
public void run() {
try {


Thread.sleep(3000);
if(mDialog.isShowing()){
mDialog.dismiss();
}
Intent intent2=new Intent(LoginActivity.this,Show.class);
startActivity(intent2);
} catch (Exception e) {
// TODO: handle exception
}
}
}.start();
}
}
isRemenber.setOnCheckedChangeListener( new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isRemenber.isChecked()==false){
isLoginSelf.setChecked(false);
}
}
});
isLoginSelf.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
isRemenber.setChecked(true);
}
});
longin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!name.getText().toString().equals()&&!pass.getText().toString().equals()){
if(isLoginSelf.isChecked()){
pre.edit().putBoolean(isrmb, true).putBoolean(islgs, true).putString(name, name.getText().toString())
.putString(pass, pass.getText().toString()).commit();
}else{
if(isRemenber.isChecked()){
pre.edit().putBoolean(isrmb, true).putBoolean(islgs, false).putString(name, name.getText().toString())
.putString(pass, pass.getText().toString()).commit();
}else{
pre.edit().putBoolean(isrmb, false).putBoolean(islgs, false).putString(name, name.getText().toString())
.putString(pass, pass.getText().toString()).commit();
}
}
Intent intent=new Intent(LoginActivity.this,Show.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), 密碼或賬號不能為空!, Toast.LENGTH_LONG).show();
}


}
});
}
private void creatDialog() {
mDialog=new ProgressDialog(this);
mDialog.setTitle(驗證中);
mDialog.setMessage(正在登陸請稍後);
mDialog.setIndeterminate(true);
mDialog.setCancelable(true);
mDialog.show();

}
}

import android.app.Activity;
import android.os.Bundle;


public class Show extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
}
}


package=alex.sias.com
android:versionCode=1
android:versionName=1.0 >









android:icon=@drawable/icon
android:label=@string/app_name
android:theme=@android:style/Theme.NoTitleBar.Fullscreen >
android:name=.LoginActivity
android:label=@string/app_name >












 

用到兩個布局文件


android:orientation=vertical
android:layout_width=fill_parent
android:layout_height=fill_parent
android:background=@drawable/chat_bg_default
>
android:layout_height=50dp
android:layout_width=fill_parent
android:gravity=center
android:textSize=24sp
android:text=登錄界面
android:textColor=@color/white
android:background=@drawable/song_index_item_bg/>
android:id=@+id/iv
android:layout_height=220dp
android:layout_width=230dp
android:layout_marginTop=110dp
android:src=@drawable/background
android:layout_centerHorizontal=true
android:scaleType=fitXY/>
android:id=@+id/tv_name
android:layout_height=wrap_content
android:layout_width=wrap_content
android:textSize=15sp
android:layout_marginTop=7dp
android:text=賬號:
android:layout_alignLeft=@+id/iv
android:layout_alignTop=@+id/iv
android:layout_marginLeft=7dp
android:textColor=@color/green/>
android:id=@+id/et_name
android:layout_height=25dp
android:layout_width=210dp
android:layout_marginTop=7dp
android:layout_below=@+id/tv_name
android:background=@drawable/shape
android:layout_alignLeft=@+id/tv_name/>
android:id=@+id/tv_pass
android:layout_height=wrap_content
android:layout_width=wrap_content
android:textSize=15sp
android:text=密碼:
android:layout_alignLeft=@+id/tv_name
android:layout_below=@+id/et_name
android:layout_marginTop=7dp
android:textColor=@color/green/>
android:id=@+id/et_pass
android:layout_height=25dp
android:layout_width=210dp
android:background=@drawable/shape
android:layout_marginTop=7dp
android:layout_below=@+id/tv_pass
android:layout_alignLeft=@+id/tv_name/>
android:id=@+id/isremenber
android:layout_height=wrap_content
android:layout_width=wrap_content
android:layout_alignLeft=@+id/tv_name
android:layout_marginTop=10dp
android:text=記住密碼
android:textSize=11sp
android:textColor=@color/huise
android:layout_below=@+id/et_pass
android:button=@drawable/mycheckbox/>
android:id=@+id/isloginself
android:layout_height=wrap_content
android:layout_width=wrap_content
android:layout_alignRight=@+id/et_name
android:layout_marginTop=10dp
android:text=自動登錄
android:textSize=11sp
android:textColor=@color/huise
android:layout_below=@+id/et_pass
android:button=@drawable/mycheckbox/>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved