安卓自动登录

<h2><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">8.MainActivity.java</span></h2>  

1.activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
02.    xmlns:tools="http://schemas.android.com/tools"  
03.    android:layout_width="match_parent"  
04.    android:layout_height="match_parent"  
05.    android:orientation="vertical"  
06.    android:background="@drawable/loginbg"  
07.    android:paddingBottom="@dimen/activity_vertical_margin"  
08.    android:paddingLeft="@dimen/activity_horizontal_margin"  
09.    android:paddingRight="@dimen/activity_horizontal_margin"  
10.    android:paddingTop="@dimen/activity_vertical_margin"  
11.    tools:context=".MainActivity" >  
12.  
13.  <include layout="@layout/login_top"/>   
14.  <include layout="@layout/login_bottom"/>"  
15.  
16.</LinearLayout>  

2.login_top.xml

<?xml version="1.0" encoding="utf-8"?>  
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
03.    android:layout_width="match_parent"  
04.    android:layout_height="wrap_content"  
05.    android:background="@drawable/btnbg_roundcorner"  
06.    android:paddingBottom="@dimen/activity_vertical_margin"  
07.    android:paddingLeft="@dimen/activity_horizontal_margin"  
08.    android:paddingRight="@dimen/activity_horizontal_margin"  
09.    android:paddingTop="@dimen/activity_vertical_margin" >  
10.  
11.    <TextView  
12.        android:id="@+id/tvUsername"  
13.        android:layout_width="wrap_content"  
14.        android:layout_height="wrap_content"  
15.        android:layout_alignParentLeft="true"  
16.        android:layout_alignParentTop="true"  
17.        android:text="@string/tvName"  
18.        android:textAppearance="?android:attr/textAppearanceMedium" />  
19.  
20.    <EditText  
21.        android:id="@+id/etUsername"  
22.        android:layout_width="match_parent"  
23.        android:layout_height="wrap_content"  
24.        android:layout_alignLeft="@+id/tvUsername"  
25.        android:layout_below="@+id/tvUsername"  
26.        android:background="@android:drawable/edit_text"  
27.        android:ems="10" >  
28.  
29.        <requestFocus />  
30.    </EditText>  
31.  
32.    <TextView  
33.        android:id="@+id/tvPassword"  
34.        android:layout_width="wrap_content"  
35.        android:layout_height="wrap_content"  
36.        android:layout_alignLeft="@+id/etUsername"  
37.        android:layout_below="@+id/etUsername"  
38.        android:text="@string/tvPassword"  
39.        android:textAppearance="?android:attr/textAppearanceMedium" />  
40.  
41.    <EditText  
42.        android:id="@+id/etPassword"  
43.        android:layout_width="match_parent"  
44.        android:layout_height="wrap_content"  
45.        android:layout_alignLeft="@+id/tvPassword"  
46.        android:layout_below="@+id/tvPassword"  
47.        android:layout_marginTop="16dp"  
48.        android:background="@android:drawable/edit_text"  
49.        android:ems="10"  
50.        android:inputType="textPassword" />  
51.  
52.    <Button  
53.        android:id="@+id/btnLogin"  
54.        android:layout_width="wrap_content"  
55.        android:layout_height="wrap_content"  
56.        android:layout_alignRight="@+id/etPassword"  
57.        android:layout_below="@+id/etPassword"  
58.        android:layout_marginTop="20dp"  
59.        android:background="#FF72CAE1"  
60.        android:text="@string/btnLogin" />  
61.  
62.    <CheckBox  
63.        android:id="@+id/cbRememberPass"  
64.        android:layout_width="wrap_content"  
65.        android:layout_height="wrap_content"  
66.        android:layout_alignLeft="@+id/etPassword"  
67.        android:layout_alignTop="@+id/btnLogin"  
68.        android:text="记住密码" />  
69.  
70.    <CheckBox  
71.        android:id="@+id/cbZddl"  
72.        android:layout_width="wrap_content"  
73.        android:layout_height="wrap_content"  
74.        android:layout_alignLeft="@+id/cbRememberPass"  
75.        android:layout_below="@+id/cbRememberPass"  
76.        android:text="自动登录" />  
77.  
78.</RelativeLayout>  

3.login_bottom.xml
<?xml version="1.0" encoding="utf-8"?>  
02.<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
03.    android:layout_width="match_parent"  
04.    android:layout_height="wrap_content" >  
05.  
06.    <TextView  
07.        android:id="@+id/tvRegist"  
08.        android:layout_width="wrap_content"  
09.        android:layout_height="wrap_content"  
10.        android:layout_alignParentLeft="true"  
11.        android:layout_alignParentTop="true"  
12.        android:layout_marginLeft="21dp"  
13.        android:layout_marginTop="18dp"  
14.        android:text="@string/tvRegister"  
15.        android:autoLink="all"  
16.        android:textColorLink="#FF0066CC" />  
17.  
18.    <ImageView  
19.        android:id="@+id/imageView1"  
20.        android:layout_width="wrap_content"  
21.        android:layout_height="wrap_content"  
22.        android:layout_alignParentBottom="true"  
23.        android:layout_alignParentRight="true"  
24.        android:layout_marginBottom="24dp"  
25.        android:src="@drawable/panda" />  
26.  
27.    <ImageView  
28.        android:id="@+id/imageView2"  
29.        android:layout_width="wrap_content"  
30.        android:layout_height="wrap_content"  
31.        android:layout_alignParentBottom="true"  
32.        android:layout_centerHorizontal="true"  
33.        android:layout_marginBottom="28dp"  
34.        android:src="@drawable/icon" />  
35.  
36.</RelativeLayout>  


4.chenggong.xml
<?xml version="1.0" encoding="utf-8"?>  
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
03.    android:layout_width="match_parent"  
04.    android:layout_height="match_parent"  
05.    android:orientation="vertical" >  
06.  
07.    <TextView  
08.        android:id="@+id/textView1"  
09.        android:layout_width="wrap_content"  
10.        android:layout_height="wrap_content"  
11.        android:text="登陆成功"  
12.        android:textAppearance="?android:attr/textAppearanceLarge" />  
13.  
14.</LinearLayout>  


5.btnbg_roundcorner.xml
<?xml version="1.0" encoding="utf-8"?>  
02.<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
03.    <solid android:color="#55FFFFFF"/>  
04.    <corners android:radius="10dp"/>  
05.</shape>  


6.loginbg.xml
<?xml version="1.0" encoding="utf-8"?>  
02.<shape xmlns:android="http://schemas.android.com/apk/res/android" >  
03.    <solid android:color="#55FFFFFF"/>  
04.    <corners android:radius="10dp"/>  
05.</shape>  

7.strings.xml
<?xml version="1.0" encoding="utf-8"?>  
02.<resources>  
03.  
04.    <string name="app_name">MiniTwitterSimulate</string>  
05.    <string name="action_settings">Settings</string>  
06.    <string name="hello_world">Hello world!</string>  
07.    <string name="tvName">用户名</string>  
08.    <string name="tvPassword">密码</string>  
09.    <string name="btnLogin">登录</string>  
10.    <string name="tvRegister">没有帐号? <a href="#" mce_href="#">注册</a></string>    
11.</resources>  

<h2><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">8.MainActivity.java</span></h2>
package com.example.minitwittersimulate;

import android.os.Bundle;
import android.app.Activity;
import android.app.backup.SharedPreferencesBackupHelper;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
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.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
	private EditText yhm;
	private EditText mima;
	private CheckBox jzmm;
	private CheckBox zddl;
	private Button dl;
	private SharedPreferences sharedPreferences = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		yhm = (EditText)findViewById(R.id.etUsername);
		mima = (EditText)findViewById(R.id.etPassword);
		jzmm = (CheckBox)findViewById(R.id.cbRememberPass);
		zddl = (CheckBox) findViewById(R.id.cbZddl);
		dl = (Button)findViewById(R.id.btnLogin);		
		setListener();
	}
	private void setListener() {
		if(zddl.isChecked()){
			if("huhuaiyuan".equals(yhm.getText().toString())&& "123456".equals(mima.getText().toString())){
			Intent i = new Intent(MainActivity.this,chenggong.class);
			startActivity(i);
		}else{
				Toast.makeText(getApplicationContext(),"应户名或密码错误", Toast.LENGTH_SHORT).show();
		}
		}else{
		dl.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if("huhuaiyuan".equals(yhm.getText().toString())&& "123456".equals(mima.getText().toString())){
					if(jzmm.isChecked()){
						sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
						Editor edit = sharedPreferences.edit();
						edit.putBoolean("isKeep", true);
						edit.putString("yhm",yhm.getText().toString());
						edit.putString("mima", mima.getText().toString());
						edit.commit();
					}else{
						SharedPreferences sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
						Editor edit = sharedPreferences.edit();
						edit.putBoolean("isKeep", false);
						edit.putString("yhm","");
						edit.putString("mima","");
						edit.commit();
					}
					Intent i = new Intent(MainActivity.this,chenggong.class);
					startActivity(i);
				}else{
					Toast.makeText(getApplicationContext(),"应户名或密码错误", Toast.LENGTH_SHORT).show();
				}
			}
		});}
		
	}
	protected void onResume(){
		super.onResume();
		getData();
	}

	private void getData() {
		 sharedPreferences = getSharedPreferences("data",MODE_PRIVATE);
		 if(sharedPreferences.getBoolean("isKeep",false)){
			 yhm.setText(sharedPreferences.getString("yhm",""));
			 mima.setText(sharedPreferences.getString("mima",""));
		 }
		 else{
			 yhm.setText("");
			 mima.setText("");
		 }
		
	}
	@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;
	}

}


8.chenggong.java
package com.example.minitwittersimulate;

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

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

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值