1.
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();
}
}
2.
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);
}
}
3.完成登陆
<?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:orientation="vertical"
android:background="#ffebcd">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="王贝贝"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_margin="100dp"/>