zoukankan      html  css  js  c++  java
  • 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);
    }
    }

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="alex.sias.com"
        android:versionCode="1"
        android:versionName="1.0" >


        <uses-sdk android:minSdkVersion="8" />


        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
        </uses-permission>


        <application
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <activity
                android:name=".LoginActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />


                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="Show" >
            </activity>
        </application>


    </manifest>

    用到两个布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/chat_bg_default"
        >
        <TextView 
        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"/>
        <ImageView 
        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"/>
        <TextView 
        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"/>
        <EditText 
        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"/>
        <TextView 
        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"/>
        <EditText 
        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"/>
        <CheckBox 
        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"/>
        <CheckBox 
        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"/>
        <Button 
        android:id="@+id/longin"
        android:layout_height="25dp" 
        android:layout_width="210dp"
        android:layout_below="@+id/isloginself"
        android:layout_alignLeft="@+id/et_pass"
        android:layout_marginTop="10dp"
        android:text="登录"
        android:gravity="center"
        android:background="@drawable/loginbtn"/>
    </RelativeLayout>

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/white">
      <TextView 
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:text="嘿嘿!"
      android:textSize="40sp"
      android:gravity="center"/>
        
    </LinearLayout>

  • 相关阅读:
    vscode 前端好用插件汇总
    IE8和IE9下textarea滚动选中的问题
    javascript实现数字整数位每三位一个逗号分隔
    简单枚举(算法竞赛入门经典)
    拓扑排序(算法竞赛入门经典)
    七桥问题--欧拉(算法竞赛入门经典)
    走迷宫问题 算法竞赛入门经典
    ZOJ1008
    ZOJ1163
    HDU 1069 Monkey and Banana
  • 原文地址:https://www.cnblogs.com/lechance/p/4373333.html
Copyright © 2011-2022 走看看