//布局
<RelativeLayout 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"
tools:context="${relativePackage}.${activityClass}"
android:background="@drawable/a" >
<Button
android:id="@+id/btn_times"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@null"
android:text="跳转5" />
</RelativeLayout>
//页面赋值 Handler
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btn_times;
// 定义临时变量
int t = 5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_times = (Button) findViewById(R.id.btn_times);
btn_times.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 为0时跳转
t = 0;
}
});
handler.sendEmptyMessage(0);
btn_times.setText("跳转" + t);
}
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
if (t<=0) {
startActivity(new Intent(MainActivity.this,TwoActivity.class));
finish();
}else {
t--;
btn_times.setText("跳转" + t);
Message message = new Message();
message.what = 0;
handler.sendMessageDelayed(message, 1000);
}
break;
default:
break;
}
};
};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}
//登录页面的布局
<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:background="@drawable/a"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="200dp"
android:gravity="center"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入账号" />
<EditText
android:id="@+id/edit_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="请输入密码" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/check_jz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码" />
<CheckBox
android:id="@+id/check_zd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自动登录" />
</LinearLayout>
<Button
android:id="@+id/btn_deng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@null"
android:layout_marginTop="20dp"
android:text="登录" />
</LinearLayout>
//存储数据 下次登录不在显示
package com.example.yuekao5;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
public class TwoActivity extends FragmentActivity {
private SharedPreferences sharedPreferences;
private EditText edit_name;
private EditText edit_pass;
private CheckBox check_jz;
private CheckBox check_zd;
private Button btn_deng;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
edit_name = (EditText) findViewById(R.id.edit_name);
edit_pass = (EditText) findViewById(R.id.edit_pass);
check_jz = (CheckBox) findViewById(R.id.check_jz);
check_zd = (CheckBox) findViewById(R.id.check_zd);
btn_deng = (Button) findViewById(R.id.btn_deng);
sharedPreferences = getSharedPreferences("1606B", MODE_PRIVATE);
boolean isJZ = sharedPreferences.getBoolean("isJZ", false);
String username = sharedPreferences.getString("username", "");
String password = sharedPreferences.getString("password", "");
if (isJZ) {
edit_name.setText(username);
edit_pass.setText(password);
check_jz.setChecked(true);
}
boolean isZD = sharedPreferences.getBoolean("isZD", false);
if (isZD) {
startActivity(new Intent(TwoActivity.this, ThreeActivity.class));
finish();
}
btn_deng.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 获取账号密码
String username = edit_name.getText().toString().trim();
String password = edit_pass.getText().toString().trim();
if (check_jz.isChecked()) {
Editor editor = sharedPreferences.edit();
editor.putString(username, "username");
editor.putString(password, "password");
editor.putBoolean("isJZ", true);
editor.commit();
}
if (check_zd.isChecked()) {
Editor editor = sharedPreferences.edit();
editor.putBoolean("isZD", true);
editor.commit();
}
startActivity(new Intent(TwoActivity.this, ThreeActivity.class));
finish();
}
});
}
}