手机登录

<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif;">运行结果如下:</span>

登陆界面:

记住密码:

跳转界面:

自动登录:

观看自动登录结果:

 要实现一个手机记住密码和自动登录界面,步骤如下,首先先看一下需要建立一下类;

第一步:在res/layout/下建立activity_main.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=".MainActivity" >
  <include layout="@layout/login_top"/>
  <include layout="@layout/login_bottom"/>"
</LinearLayout>


 

第二步:在res/layout下建立login_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>


第三步:在res/layout下建立login_top.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"
    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:layout_marginTop="16dp"
         android:background="@android:drawable/edit_text"
        android:ems="10"
        android:inputType="textPassword" />
    <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:layout_marginTop="20dp"
        android:onClick="to2"
        android:background="#9A32CD"
        android:text="@string/btnLogin" />
    <CheckBox
        android:id="@+id/cbRememberPass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        android:layout_alignLeft="@+id/etPassword"
        android:layout_alignTop="@+id/btnLogin"
        android:text="记住密码" />
    <CheckBox
        android:id="@+id/autologin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/cbRememberPass"
        android:layout_alignBottom="@+id/cbRememberPass"
        android:layout_toRightOf="@+id/cbRememberPass"
        android:text="自动登录" />
</RelativeLayout>


第四步:在res/layout下建立newpager.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="#B23AEE"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_margin="80dp"
        android:layout_weight="10"
        android:text="张桂云!!!"
        android:textSize="40dp" />
</LinearLayout>


第五步:在src下建立MainActivity.java文件

package com.example.minitwittersimulate;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
 private EditText name;
 private EditText pass;
 private CheckBox isRemenber;
 private CheckBox isLoginSelf;
 private Button longin;
 private ProgressDialog mDialog;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.activity_main);
  name=(EditText) findViewById(R.id.etUsername);
  pass=(EditText) findViewById(R.id.etPassword);
  isRemenber=(CheckBox) findViewById(R.id.cbRememberPass);
  isLoginSelf=(CheckBox) findViewById(R.id.autologin);
  longin=(Button) findViewById(R.id.btnLogin);
  final SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
  if(sharedPreferences!=null)
  {
   if(sharedPreferences.getBoolean("isrmb",false)==true)
   {
    name.setText(sharedPreferences.getString("name",null));
    pass.setText(sharedPreferences.getString("pass",null));
    isRemenber.setChecked(true);
   }
   if(sharedPreferences.getBoolean("islgs",false)==true)
   {
    isLoginSelf.setChecked(true);
    ceratDialog();
    new Thread()
    {
     public void run()
     {
      try{
       Thread.sleep(3000);
       if(mDialog.isShowing())
       {
        mDialog.dismiss();
       }
       Intent intent2=new Intent(MainActivity.this,newpager.class);
       startActivity(intent2);
       //finish();
      }catch(Exception e)
      {
       
      }
     }
    }.start();
   }
  }
  isRemenber.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // TODO Auto-generated method stub
    if(isRemenber.isClickable()==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())
     {
      sharedPreferences.edit().putBoolean("isrmb",true).putBoolean("islgs",true).putString("name",name.getText().toString()).putString("pass",pass.getText().toString()).commit();
     }
     else
      if(isRemenber.isChecked())
      {
       sharedPreferences.edit().putBoolean("isrmb",true).putBoolean("islgs",false).putString("name",name.getText().toString()).putString("pass",pass.getText().toString()).commit();
      }
      else
      {
       sharedPreferences.edit().putBoolean("isrmb",false).putBoolean("islgs",false).putString("name",name.getText().toString()).putString("pass",pass.getText().toString()).putString("pass",pass.getText().toString()).commit();
      }
     Intent intent=new Intent(MainActivity.this,newpager.class);
     startActivity(intent);
     finish();
    }
    else{
     Toast.makeText(getApplicationContext(), "密码或账号不能为空!",Toast.LENGTH_LONG).show();
    }
     
   }
  });
 }
 
 
/** public void to2 (View view)
 {
  Intent intent=new Intent();
  intent.setClass(this,welcome.class);
  startActivity(intent);
 }
 **/
/** public void save (View view)
 {
  SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
  Editor editor=sharedPreferences.edit();
  editor.putString("username","user");
  editor.putString("password","password");
  editor.commit();
 }
**/
 private void ceratDialog()
 {
  mDialog=new ProgressDialog(this);
  mDialog.setTitle("验证中");
  mDialog.setMessage("正在登陆请稍后");
  mDialog.setIndeterminate(true);
  mDialog.setCancelable(true);
  mDialog.show();
  
 }
}


 

第六步:在src下建立newpager.java文件

package com.example.minitwittersimulate;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class newpager extends Activity {
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  setContentView(R.layout.newpager);
 }
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值