编写不易,如有转载,请声明出处: 梦回河口:http://blog.csdn.net/zxc514257857/article/details/69219113
//(layout)activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.qqlogin.MainActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_weight="1"
android:background="#11B6F4"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageView
android:src="@mipmap/pic"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
RelativeLayout>
<RelativeLayout
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<RelativeLayout
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:paddingLeft="10dp"
android:id="@+id/et_username"
android:layout_width="400dp"
android:layout_height="30dp"
android:background="@drawable/edittext_shape"
android:singleLine="true"
android:hint="QQ号码/手机/邮箱"/>
<EditText
android:layout_marginTop="5dp"
android:paddingLeft="10dp"
android:layout_below="@+id/et_username"
android:id="@+id/et_password"
android:inputType="textPassword"
android:layout_width="400dp"
android:layout_height="30dp"
android:background="@drawable/edittext_shape"
android:singleLine="true"
android:hint="密码"/>
<CheckBox
android:id="@+id/cb_savePassword"
android:layout_marginTop="5dp"
android:textSize="18sp"
android:text="记住密码"
android:layout_below="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_below="@+id/cb_savePassword"
android:layout_marginTop="5dp"
android:layout_width="400dp"
android:layout_height="30dp">
<Button
android:id="@+id/login"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="确 定"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:background="@drawable/button_selector"
android:textColor="#fff"
android:onClick="login"/>
<Button
android:id="@+id/logout"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="退 出"
android:textSize="20sp"
android:layout_alignParentRight="true"
android:background="@drawable/button_selector"
android:textColor="#fff"
android:onClick="logout"/>
RelativeLayout>
RelativeLayout>
RelativeLayout>
LinearLayout>
-----------------------------------------------------------
//(layout)activity_second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="@mipmap/p1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
LinearLayout>
-----------------------------------------------------------
//(drawable)button_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="10px"/>
<solid android:color="@color/colorYellow"/>
<stroke android:width="1px" android:color="#FFF"/>
shape>
item>
<item android:state_pressed="false">
<shape android:shape="rectangle">
<corners android:radius="10px"/>
<solid android:color="@color/colorBlue"/>
<stroke android:width="1px" android:color="#FFF"/>
shape>
item>
<item android:state_selected="true">
<shape android:shape="rectangle">
<corners android:radius="10px"/>
<solid android:color="@color/colorYellow"/>
<stroke android:width="1px" android:color="#FFF"/>
shape>
item>
<item android:state_selected="false">
<shape android:shape="rectangle">
<corners android:radius="10px"/>
<solid android:color="@color/colorBlue"/>
<stroke android:width="1px" android:color="#FFF"/>
shape>
item>
selector>
-----------------------------------------------------------
//(drawable)edittext_shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#fff"/>
<corners android:radius="8px"/>
<stroke android:color="#2BA6E1"
android:width="2px"/>
shape>
//MainActivity
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
private EditText mEt_username;
private EditText mEt_password;
private CheckBox mCb_savePassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//强制设置为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
initView();
initData();
exitActivity();
}
private void exitActivity() {
ExitApplication.getInstance().addActivity(MainActivity.this);
}
private void initView(){
mEt_username = (EditText) findViewById(R.id.et_username);
mEt_password = (EditText) findViewById(R.id.et_password);
//设置密码输入模式
mEt_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
mCb_savePassword = (CheckBox) findViewById(R.id.cb_savePassword);
}
//进入程序 如果有缓存 就读取缓存的帐号密码
private void initData(){
//帐号密码保存在内存缓存中
File file = new File(this.getCacheDir(),"QQLogin.txt");
read(file);
}
private void read(File file) {
//帐号密码保存在SDcard中
//File file = new File(Environment.getExternalStorageDirectory(),"QQLogin.txt");
if(file.exists() && file.length() > 0){
try {
//读流
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String QQLogin = br.readLine();
//帐号密码用##分隔,所以用##切割
String username = QQLogin.split("##")[0];
String password = QQLogin.split("##")[1];
//并将缓存的帐号密码显现
mEt_username.setText(username);
mEt_password.setText(password);
mCb_savePassword.setChecked(true);
fis.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void login(View view) {
String username = mEt_username.getText().toString().trim();
String password = mEt_password.getText().toString().trim();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "帐号或密码为空,请重新输入!", Toast.LENGTH_SHORT).show();
return;
}
if (username.equals("123456") && password.equals("123456")) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
checkAndWrite(username, password);
}
private void checkAndWrite(String username , String password) {
if (mCb_savePassword.isChecked()) {
Toast.makeText(this, "记住密码!", Toast.LENGTH_SHORT).show();
//帐号密码保存在内存缓存中
File file = new File(this.getCacheDir(),"QQLogin.txt");
try {
//写流
FileOutputStream fos = new FileOutputStream(file);
fos.write((username + "##" + password).getBytes());
Toast.makeText(this, "数据保存成功", Toast.LENGTH_SHORT).show();
fos.close();
} catch (Exception e) {
Toast.makeText(this, "数据保存失败", Toast.LENGTH_SHORT).show();
e.printStackTrace();
Log.i("Main", e.toString());
}
} else {
Toast.makeText(this, "取消密码!", Toast.LENGTH_SHORT).show();
File file = new File(this.getCacheDir(),"QQLogin.txt");
if(file.exists() && file.length() > 0){
file.delete();
}
}
}
//点击登录按钮
public void logout(View view){
needEnterPwd();
}
//点击返回按钮
public void onBackPressed(){
needEnterPwd();
}
//需输入密码
private void needEnterPwd() {
final EditText password = new EditText(this);
password.setFocusable(true);
//设置为密码不可见形式
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请输入退出密码")
.setIcon(R.mipmap.ic_launcher)
.setNegativeButton("取消", null)
.setCancelable(false)
.setView(password)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String inputName = password.getText().toString();
if(inputName.equals("123456")){
//退出所有Activity操作
ExitApplication.getInstance().exit();
}else{
new AlertDialog.Builder(MainActivity.this)
.setTitle("警告")
.setIcon(R.mipmap.ic_launcher)
.setMessage("密码输入错误,请重新输入!")
.setCancelable(false)
.setNegativeButton("取消",null)
.setPositiveButton("确定",null)
.show();
}
}
}).show();
}
}
-----------------------------------------------------------
//SecondActivity
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//强制设置为横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
exitActivity();
}
private void exitActivity() {
ExitApplication.getInstance().addActivity(SecondActivity.this);
}
}
-----------------------------------------------------------
//ExitApplication
import android.app.Activity;
import android.app.Application;
import java.util.ArrayList;
import java.util.List;
public class ExitApplication extends Application {
private List activityList = new ArrayList<>();
private static ExitApplication instance;
public ExitApplication(){}
public static ExitApplication getInstance(){
if(null == instance){
instance = new ExitApplication();
}
return instance;
}
//添加Activity到容器中
public void addActivity(Activity activity){
activityList.add(activity);
}
//遍历所有Activity并finish
public void exit(){
for(Activity activity : activityList){
//依次关闭
activity.finish();
}
//强制退出
System.exit(0);
}
}
Demo下载请移步:http://download.csdn.net/detail/zxc514257857/9833032
———-因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!———-