MiniTwitter登入界面记住密码

来源:互联网 发布:seo外包公司 小周 编辑:程序博客网 时间:2024/04/29 05:07

1先建立一个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> 
2:在建立login_botton.xml , 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" > 
 
    <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>

<?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:background="#FF72CAE1" 
        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="记住密码" /> 
 
</RelativeLayout> 

3JAVA代码:

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.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.Menu;
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 Password;
 private CheckBox isRemember;
 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);
  Password=(EditText) findViewById(R.id.etPassword);
  isRemember=(CheckBox) findViewById(R.id.cbRememberPass);
  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));
    Password.setText(sharedPreferences.getString("Password",null));
    isRemember.setChecked(true);
   }
   if(sharedPreferences.getBoolean("islgs",false)==true)
   {
    
    ceratDialog();
    new Thread()
    {
     public void run()
     {
      try{
       Thread.sleep(3000);
       if(mDialog.isShowing())
       {
        mDialog.dismiss();
       }
       Intent intent2=new Intent(MainActivity.this,welcome.class);
       startActivity(intent2);
       //finish();
      }catch(Exception e)
      {
       
      }
     }
    }.start();
   }
  }
  
       isRemember.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
     // TODO Auto-generated method stub
     
     isRemember.setChecked(true);
     
     
    }
   });
   longin.setOnClickListener(new OnClickListener() {
                        @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if(!(TextUtils.isEmpty((CharSequence) name)) && !(TextUtils.isEmpty((CharSequence) Password))){
     if("admin".equals(name)&&"123456".equals(Password)){
      Editor editor=sharedPreferences.edit();
      if(isRemember.isChecked()){
       editor.putString("name", name.getText().toString());
       editor.putString("Password", Password.getText().toString());
       editor.putBoolean("isRemember", true);
      }else{
       editor.clear();
      }
      editor.commit();
      Intent intent=new Intent(MainActivity.this,welcome.class);
      startActivity(intent);
      finish();
     }else{
      Toast.makeText(getApplicationContext(), "密码或账号不为空!", Toast.LENGTH_LONG).show();
     }
    }
    
    
    
   }
   });
  }
 

 private void ceratDialog() {
  // TODO Auto-generated method stub
  
 }


 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }
 
}

 

0 0
原创粉丝点击